So in our application, we've been using the built in HTML template package, which is part of the standard
library and go, and I think it's important to know how that library works.
But those of you that are coming to Web development and go from other languages might find that the
syntax for templates in goaland is a little awkward and it is, but it gets the job done and you get
used to it very quickly.
And I think it's very important that you know what it is.
Having said that, there are other template systems out there and there are many to choose from.
You can actually just Google Goaland template engines and you'll get a list of a whole bunch of them.
I want to draw your attention to one, and I want to show you a very, very simple application that
uses this templating system.
So the templating system is found here, GitHub, dot com, cloudy kett with a capital C and a capital
K jet.
And it's been around for a while.
It's up to version six.
As I record this and I use it all the time, I use sometimes I use go templates, sometimes I use cloud
tickets, sometimes I use something else.
But Jet is something that many of you who are used to using, say, blade templates and Laravel might
be find this a little more useful.
It's very, very simple to implement.
So you can read all the documentation here and I'll show you how it works.
So they actually have a syntax reference, a nice wiki.
Some things are out of date, but it's easy to figure it out and I'll show you how it works here anyway.
So I'm going to go back to my application and just take you through the code.
So very simple app.
I'm in my main package, which is in command Web main, and I just define some routes.
And for this one, I used the I think I use Pat.
Let's find the routes and find out.
Yes, I use the pat the pat router, which I went through in this course already.
And I just define two routes slash, which is the home page and send, which is just an empty page.
I needed to put something there.
So I put send so back to our main goal.
Once we do this we actually listen and serve and we listen on Port AT&T, as we've been doing in our
course.
So let's go look and see what some of these handlers, how they work.
OK, so I'll go to the homepage and the home page.
This is the handler for the home page.
And just as it does in our course, it takes a response writer and a pointer to a request.
And then I just built up some data, just dummy data to send to a template.
Let me space this out a bit so you can see a little better.
So I start by defining a jet versus map variable.
OK, so I just make that and it holds anything I want.
So once I've created this variable called data, I call in the builtin method.
I call the builtin method set and I give it a name.
This is what I'm going to refer to it in using my jet template.
And I just store some value in there and I can put whatever I want in there.
So initially I just said user ID is equal to one, then I set another one first for first name is equal
to a string, so I put it into one, I put a string in, then I define a slice of strings, which are
the days of the week, and I set a variable in my data called DSW four days of week and passed in that
slice of strings.
Then I called Render Page and I'm passing that three things.
I'm passing it the response rate, which I need to write to.
That's what's going to be how we render information to the end user and their web browser.
The name of the template I want to load and then the data.
Now, this render page is not part of the template package.
Just to be clear, I'm just calling my own function, which is right down here.
And all I do is I have those three arguments and I create I have a variable called views and I call
get template.
Now, view is way up here and that's a jetset.
And this is what you have to have to start with to make sure that you can actually use the jet template
engine.
And it's really, really simple.
So I say, first of all, there's an OS file system loader.
Look at the root level of the application in a folder called HTML.
And over here I have an HTML folder that has two files home and sent the second one I put in development
mode because I want to be able to load changes to my template without starting or stopping and restarting
the application, kind of like the cache variable we used in our system.
So I have this variable views down here.
I say view error is equal to or assign the value of views, get the template wherever it is.
Now, I passed homecourt jet here.
So it goes to the file system.
It looks in the directory I specified, which I called HTML and it looks for a file called Homeport
Jet, which is the argument I passed.
So it can't get that.
It just returns an error and logs the error.
Otherwise I execute that view, that homecourt jet and I pass it my response writer, because it gives
it something to write to the data, which is that jet set variable that I defined a little while ago
and put a few values.
And the third one is actually context.
I'm not going to bother going through that.
You can read it yourself.
It's another place to store information and it uses the context to store that that executes that template
if it can't execute it, if it turns an error.
So let's start this application, go run command web main or start go try that again.
Stargirl starts the application, listens on Port AT&T.
I go back to my Web browser, I go to a tab and I go to localhost AT&T and I'm not going to put anything
in there.
I want to display the homepage and there it is.
It displays at all very, very simple.
Let's go look at the template and see how that how that is different from the built in go templates.
It's actually pretty simple.
So let me go to the homepage and hide this and have a look.
So this is just a simple HTML page.
I just load bootstraps success and bootstraps JavaScript, which I'm not even using, but I load it
and then I just say this is the homepage and then user ID.
I just refer to user ID with no dot.
That's one difference right away.
And the first name is first with no dot and ranging over things range key value is a the value of and
that's pretty straightforward.
Now the syntax is actually a little simpler than it is in the goal line templates and you have some
built in functions as well.
So let's go back and look at those cloudy kit will go to the wiki.
Some things are out of date, but not too many.
Someday I'm going to go rewrite his documentation and give it to him.
So built in functions, for example, you get some nice simple built in functions to change things to
lowercase, to uppercase, to trim space, check, to see if a variable is actually there or not, which
a lot of people miss and go like templates, including me.
And you get all of these other ones as well.
And the documentation is really, really easy to work with.
So, for example, let's see if we can find decisions, if then else here we are if expression and see
how easy this is.
And it's simpler than using the ECU or any or all of those little comparison operators that you have
in the go line templates.
Now, it sounds as though I'm suggesting you use jet templates instead of the built in go templates,
and nothing could be further from the truth.
I think it's really important that people coming to go for the first time become used to the built in
templating syntax because you're going to run into it in other projects.
But once you've mastered that or you're really comfortable with it, if you want to use something like
Jet, there's nothing to stop you from doing that.
And a lot of people find it easier.
Also, templated inheritance is built in all those things that you really expect to find from a good
templating package.
All right, one other thing.
I want to draw your attention.
So this is my homepage.
Here's one thing, one compelling reason why a lot of people come here.
Let me define or refer to a variable ABC that I know doesn't exist, and I'll save that.
And I'll go back to my.
Web browser right here, find my home page.
Let me reload this, and now it gives me a meaningful error message which is conspicuous by its absence
in the built in go templating system.
And this, if nothing else, is extremely helpful.
So there you go.
I just wanted to draw your attention to this.
And if you really like jump chat templates and want to use those instead of the ones that I'm using
for the remainder of this course, feel free to do so.
But I would strongly encourage you to leave your experimentation with go templates to a separate project
and follow along with the way that I'm doing it in this course or finish the course and then rewrite
the pages in Jet, which is not a terribly onerous task.