Check out a free preview of the full Go for JavaScript Developers course:
The "Go Templates" Lesson is part of the full, Go for JavaScript Developers course featured in this preview video. Here's what you'd learn in this lesson:

Brenna creates another endpoint for the todo application, and uses Go templates to generate HTML output on a webpage. How to send the appropriate server error to the webpage when a template cannot be parsed is also covered.

Get Unlimited Access Now

Transcript from the "Go Templates" Lesson

>> Let's create a second route handler. Let's call it /todos. We're going to do kind of the same thing, build a route handler that intercepts the route todos. And then in that route handler function, just print something to console for now and we'll flesh that out in just a couple of minutes.

[00:00:16] Okay, so let's implement our todos endpoint, we're going to set up a new route handler. HandleFunc, sending it to our /todos/. And we're gonna send it to todos function. So up here we'll create a func todos and pass it our two arguments, our writer and our request. But instead of printing to the console, we're going to use a different version of the func print functionality.

[00:00:46] So we're gonna say func.fprint, and we're gonna pass it two things. So this is going to print to an external writer. So the first thing we need to tell it is where we wanna print to the text to and then what we wanna print. So we'll save that make sure to kill your server and restart it.

[00:01:05] And we navigate to our browser and refresh, you'll see that now we're seeing that text actually print to our browser. Okay, we're gonna add some functionality in our todo function to actually start being able to request get todos, add todos, etc. So your repository is actually set up with a little bit of code to get us started.

[00:01:24] So if you hop over to the repository at the bottom, we have a todos.html file. So within this file, we'll just walk through kind of what exists so far. Everything lives here, all the CSS lives here. We're just gonna use this for now even though in real life, you'd probably have a different structure going on.

[00:01:41] At the top of the HTML file is a series of CSS so that things look a little more styled. But then the part that's interesting is in the body of the HTML. So you'll notice that we have an h1, we have a form, and then we have some divs that are happening with some information within them.

[00:02:01] And so we're gonna ignore this for just a second. We're gonna see it render in the browser and then let's circle back and talk about why these variables are here and what they're doing. So in our server.go file, we're gonna modify our todos function. I'll kill the print line.

[00:02:17] And then here we're going to use the template.parse files function. And we're gonna pass the file todos.html into template.parse files. So notice that we're calling a method on a library we haven't imported yet. So we need to scroll up to the top here and make sure we're adding template to our list of imports.

[00:02:41] And is html/template, not just template. And then template.parse files is going to return two different things. It's gonna return whatever was just parsed and an error if there is one. So you can define those two variables on the left hand side and assign the value to whatever we get back from our template.parse files method.

[00:03:03] And we're getting an error, we're not using either the t or the err, so we need to add some code here. So underneath our parse files, and we're passing it that todos.html that we just looked at. The first thing I wanna check is if there's an error. So we'll handle our error using the syntax we learned earlier today.

[00:03:22] Which is, if error does not equal nil, let's send the error to our HTTP package. Where do we want to write it to? err.error will give us back that error message, and then the status. And you'll notice that my code editor here is giving me like a preemptive list of kind of different HTTP statuses we could send back.

[00:03:45] For today, we're just going to kind of hang out in the status of that request. But if you wanted to get really specific about what error code you want to send back for this error, you can get more specific there. So if there's an error, this is what we wanna do about it.

[00:04:02] I'm also just going to print out template parsing error and the error itself just so we can kind of see what's happening using the log library. So if you go up to the top, make sure that the log library is also in your imports. And then underneath our if error block, we're going to say reassign the error to t.execute.

[00:04:32] So on line 15, we've established that if we parse the file successfully, what we get back will be assigned to that variable t. And so if there's no error, we wanna execute on that variable t to the writer. And the second argument can be no, which is if we need additional data being passed through.

[00:04:52] So now our todos function is doing a few things. It's parsing the HTML file. It's handling an error. And assuming everything goes well, it's writing what it gets from that HTML file back to our writer argument, which is here, our response writer. So let's save, and we can restart our server.

[00:05:14] Navigate over to our browser. And when I refresh, you'll see all of the HTML that we parsed from that file rendered to the browser.