So we left off here last time and what we did was we created this nice render template test function
to actually go and pass all of our templates, including the layouts, and store them in this variable.
My cache and my cache is a map and I can look up values in that map by a string and the string in this
case, the thing I look up values in the map by happens to be the name of the template.
So something like about page temple or homepage dot temple.
And once I look them up I actually get a pointer to that ready to use template, which is exactly what
I need to render pages to the site.
So this actually creates that cache, that, that map that I can use that I need to be able to use.
Where can I use that?
Well, the logical place to use that, of course, is here in this render template function.
But the first thing I'm going to do is get rid of all of this code that did nothing more than show that
that that we were building the cache properly.
That's of no value to us.
So let's get rid of it so we can get rid of that there.
And then I have to re initialize error because it's the first time I'm using it and there's a line down
here where I can get rid of that as well.
All right.
So we're I mean, this is the logical place to use that map of of templates, our template cache.
So I need to get this value that's produced right here, up here.
So the obvious thing to do is to call this function.
But first, let's give it a more meaningful name rather than calling it render template test, which
is not a great name.
Let's call it create template cache.
And I'm not ever using this response rate or as a parameter, so I'm going to delete that for now.
I might need it later on.
If I do, I'll add it back in.
So up here, I probably should give this a comment.
So let's do that now to get it out of the way.
Creates a template cache as a map.
All right, I need called here.
That's all I need to do.
And this takes no parameters now because we got rid of the response rate and it returns a map and an
error.
So it returns to things.
So let's just call it simple enough.
We'll call our template cache T.C. and we'll check for an error.
And that's a sign the value of create template cache.
And then we have to check for an error.
If error is not equal to nil, then I actually want to die at this point.
And there's better error handling we'll be looking at later on.
But for this, for the purposes of what we're trying to do right now, we're just going to die.
So I'll say log fatele and an error.
And that stops the application at this point because we can't get our template cache, therefore we
can't show any pages.
There's no point in going any further.
So assuming we get past this, which we will because we know this works, how are we going to actually
do something with an individual template when we pull that value out of the map?
So I could just say my template is equal to and and pull it out of the map.
So my map is called T.C. and I could pull the template name, which we know is called Tapio, the variable
here, so we could copy that and paste it in here and that will actually work.
But what if the template I'm trying to pass doesn't exist?
So I'm passing it instead of passing about page up to Templer home page that template.
What if the parameter is something like Yellow Pages template, which doesn't exist anywhere in the
map?
Well, one of the very common ways of checking for this sort of thing is actually to assign to variables
here.
So what will happen is if the template I'm trying to pull out exists, then TI will have a value and
this will be set to true.
But if it doesn't exist, this won't have any useful value and this will be default.
So I can, this will be set defaults so I can check for that by saying if not OK then what I want to
do.
Well if I can't find the template again for the purposes of what we're doing right now, I'm just going
to die.
So if we get past this, then we have our template cache.
If we get past this, then we have the template we want to use.
So how do we use a template that's not read from disk?
What we were doing before down here and all of this stuff we're going to get rid of very shortly is
we actually read the template from the disk, then we pass the template and we're done.
In this case, we can't do that.
And we can't do that for a very simple reason because we haven't read it from disk.
So we need to handle a little bit differently.
And what I'm going to do here is create something called a bytes buffer, and I'm going to give it a
name and that's going to be assigned the value of new bytes dot buffer.
So this is a buffer that will hold the information.
All of this can do is hold bytes.
And I need to put that value, that PERS template that I currently have in memory into some bytes.
So all I'm going to do is very similar to what we did before.
I'm going to not check for an error at this point equals t my template.
Execute.
And I'm going to execute.
To the buffer, the value of that template somewhat taking saying take this template that I have executed,
don't pass it any data and store the value in this buff variable.
Now, I have exactly what I need to write to my response writer so I can just say Buffer.
Right.
Try that again, Buff Dot.
Right.
And I'm going to write to my response writer, and that should write everything to the template, which
means I can get rid of this.
Now, this if I look at right to what is it written, it actually returns an error.
So technically I should say error equals and then check for that error.
If error is not equal to nil, then and I can just print the error.
What do I have here now?
It gets two returns.
And what does it return?
It returns and oh yes, the number of bytes and I don't care about the number of votes, so I'll get
rid of that.
Just ignore it.
And if there's an error at this point, I don't want the application to die.
I'm just going to write to the airlock.
So I'll just say format the print line error writing template to browser followed by the error.
All right.
In theory, this should work because I've clean things up a little bit.
And again, we're going to do improved error handling later on.
But right now, I just want to get templates written to the screen.
So let's try this.
Let's open our terminal, clear the screen and go run command web.
Start up, go.
Let's see if it runs.
That looks good.
Let's go over to our Web browser and I'm on localhost eighty eighty in the last time I was here.
Just give me a blank screen.
So this time I hope to see the homepage and there it is.
Now what about going to the about page.
That's good as well.
That's perfect.
OK, it's almost perfect because if you think about it, we've actually although we're now using layouts
and you can see that the layout is working properly.
And if I view the source from the actual page, this part comes out of the layout document.
This part comes out of the layout document.
And this is the only thing that's being rendered from the about template.
So this text here is exactly what I see here.
So everything seems to be working, but let's look at our code again.
Render Dutko.
Here we are.
What are we really doing every single time we go to the to render any page on the site?
This function is getting called render template and it receives a response writer and it receives the
name of the template I want to render.
Then every single time we open a page on the site, we're building this template cache that seems really
inefficient.
Before we made these changes, we were just loading a single template, parsing it and writing it to
the to the screen, to the browser window.
But now every single time we render a single page on the site, we're actually parsing all of our template
files.
Now, go is very fast and you're not going to notice this if you don't have any appreciable amount of
traffic on your site.
But it is grossly inefficient and there's got to be a better way to do this.
So in the next lecture, we'll figure out a better way to do that.