All right, things are starting to come along, but so far all we're doing when we show pages is writing
a little string of text to the browser window, and that's not exactly useful.
So let's look at how we can actually serve real Web pages.
And to do that, we're going to use a package that's built into the standard library called HTML template.
But before we do that, let's clean things up.
So I'm going to get rid of this inside the about handler and leave that empty.
I'll get rid of this inside the home template and leave that the home page handler and leave that empty.
I don't need these add and divide values which were serving no real purpose except to show how it functions
work.
So let's get through here and get rid of this.
And we don't have don't need this handler anymore.
So so far we just have two handlers to Handler Funk's inside the main menu that point to two functions
that do nothing.
And these functions both take a response writer and a request object, a pointer to a request object,
as all handlers must.
So now let's go back over here or delete this binary because they don't need it anymore.
And I'll create a new file and a new folder.
Actually, I'm going to create a new folder.
So new directory and I'm going to call this templates.
And this is where our HTML templates will live.
So inside of that, I'll create two templates, one called Home Page Template, and I'm going to use
that extension HTML.
If you want to use HTML or something like that, you feel free to do so.
But I like to keep things organized and point out the fact that this will eventually be a and a Goldwing
template and not a standard HTML file.
So I'll create that and I'll create another template called about vote page template.
So these are both inside of templates directory in the root project of my application.
So let's go to the home one first and I'm going to create a template.
So I'm using Emet and if you're not familiar with Emet, you should look it up.
It's a nice little extension available for both Visual Studio Code and built into GEP brains.
Langue or goaland HGP sorry HTML five and there that creates an empty template for me.
So this is a standard template.
It's just a little bit of HTML in here.
I'll just put each one.
This is the homepage just so I can tell what it is.
And over here under a boat I'll do the same thing.
HTML five and I'll call this H1.
This is the both page.
All right.
So I've created two templates.
They're just HTML.
They're not doing anything dynamic.
They will eventually.
So bear with me.
But right now I've just created those.
Now I need to tell these handlers back in main go to render those templates and I need to render them
as actual goaland templates, which means I can pass them data.
I can have dynamic content in there.
We're not going to do that in this election.
We're just this election.
We're just going to get the pages up and running.
But how do I do that?
Well, in order to do that, I need to render the template and I'm going to render a template here and
I'm going to render a template here.
And if I have a 500 more pages, I'm going to render that template 500 times.
This seems like a really good place to put a function in there.
So I'm going to create a function func render template and it's going to take two arguments, a W,
which is an al-Qaeda response writer, and it's also going to take a string naming the template I want
it to render.
So I'll call that tampoe and it's just type string and it doesn't actually return anything because it
writes everything to the the response writer.
So what am I going to do inside this function?
Well, I'm going to first of all, pass the template whenever I'm going to use a template in go like
the HTML template.
We're going to look at a different template again later on.
But right now we're going to use one that's part of the standard library.
I need to pass it.
So I'm going to create a variable past template and that is going to be equal to something.
But it also takes a second argument, which I'm getting Nawfal right now, and that is going to be assigned
the values of built into the template system right in to go template is the name of the package DOT
and I'm going to pass files.
So that's built into the application.
And a better backspace because they didn't do the import for me template dot pass files and now it's
important up here for me, so I don't have to do that manually and that just takes a path dot slash,
templates slash.
And then I'm going to append to that the name of the template.
OK, so this actually.
Loads of file from the root level of my application, which is what DOT means, so that's inside this
hollow world folder.
Inside of that, there's a directory called Templates, which is this folder right here.
And I'm going to take the argument of the template I want to pass as a string.
Great.
So I've got that.
Now, what do I do with it?
Error is assigned first template, which is the variable I just created, and it actually has some some
functions built into it.
And the one I want is execute.
So I'm going to execute that template and I'm going to pass it.
The response writer that I want it to pass the template and write to and I'm going to pass it.
No data, no OK, if error is not equal to nil.
Now my standard error, checking what I want to do.
Well, let's just write to the error, to the standard log file.
So we'll just say format, print line
and I'll pass in an error or the error message.
Error, error, error messages aren't supposed to start with capital letters, error per second template.
And then I'll actually write the error itself so I can see what it is if there happens to be one and
I'll return.
All right.
So now I have this function here render template that allows me to basically take a response writer
and the name of a template I want to pass, pass it and read it to the browser window.
So how do I do that?
Well, up here, let's give it a try in the homepage, render a template.
I'm going to call that function and it lit up there so you can see that it's actually being called now
and I'm going to pass it.
My response writer and the name of the template I want to pass, which is home page of template.
So that should actually work just for the home page.
So I'm not doing anything about yet, but let's open up a terminal window, clear the screen and run
our main go package.
Go, run, main go.
It's running, so let's go to our browser window and open up a new browser window and go to localhost
party and there it is.
So it passed that past that home page template.
So let's go back here and see if we can do something else.
I'm going to open up that homepage again and I'm going to give it a paragraph of text underneath it.
This is some text, so I save that.
Go back, reload the page and there it is.
That seems to work very well.
So we can do the same thing, of course, for the about page, which is a different template.
And I forgot to close that H1 tag, so I better do that.
Go back to my main application and I'm just going to copy this, paste it in here.
But this time I want to render the about page now because I change the actual function.
I can't just come over here and say about I can, but all it's going to give me is a blank screen because
this function has not yet been compiled.
So I need to stop the application, rerun it, go back to my browser and reload the about page.
And there's that, this is the about page.
So that's the basic logic for how we can render templates.
And it's very straightforward.
There's a as I said earlier, Go has an extremely robust and rich standard library, and one of the
things built into it is the ability to render templates with dynamic content.
Now, we haven't passed any data to these yet.
We're going to do that very shortly.
But there's a couple of other things I want to get out of the way first.
So that's how you can render HTML templates and the templates themselves have some logic built in.
You can range over data, you can make decisions about what data to show.
It's an extremely powerful system.
Now we're going to go through this templating system, the one that's built in to go first.
And then a little bit later on, we'll look at a different template engine to render our content.
But that's enough for now.
In the next lecture, we'll look at how we can structure things a little more logically, and we'll
also look at how we can pass some data to templates.
That's enough for now.