1
00:00:01,530 --> 00:00:07,290
So in our application, we've been using the built in HTML template package, which is part of the standard

2
00:00:07,290 --> 00:00:11,070
library and go, and I think it's important to know how that library works.

3
00:00:11,820 --> 00:00:17,640
But those of you that are coming to Web development and go from other languages might find that the

4
00:00:17,640 --> 00:00:23,220
syntax for templates in goaland is a little awkward and it is, but it gets the job done and you get

5
00:00:23,220 --> 00:00:24,330
used to it very quickly.

6
00:00:24,330 --> 00:00:26,430
And I think it's very important that you know what it is.

7
00:00:27,240 --> 00:00:33,150
Having said that, there are other template systems out there and there are many to choose from.

8
00:00:33,150 --> 00:00:37,340
You can actually just Google Goaland template engines and you'll get a list of a whole bunch of them.

9
00:00:37,740 --> 00:00:43,140
I want to draw your attention to one, and I want to show you a very, very simple application that

10
00:00:43,140 --> 00:00:44,700
uses this templating system.

11
00:00:45,120 --> 00:00:51,570
So the templating system is found here, GitHub, dot com, cloudy kett with a capital C and a capital

12
00:00:51,570 --> 00:00:53,280
K jet.

13
00:00:53,760 --> 00:00:55,020
And it's been around for a while.

14
00:00:55,020 --> 00:00:56,220
It's up to version six.

15
00:00:56,220 --> 00:01:01,780
As I record this and I use it all the time, I use sometimes I use go templates, sometimes I use cloud

16
00:01:01,780 --> 00:01:03,480
tickets, sometimes I use something else.

17
00:01:03,900 --> 00:01:10,290
But Jet is something that many of you who are used to using, say, blade templates and Laravel might

18
00:01:10,290 --> 00:01:11,720
be find this a little more useful.

19
00:01:11,820 --> 00:01:13,560
It's very, very simple to implement.

20
00:01:13,570 --> 00:01:16,820
So you can read all the documentation here and I'll show you how it works.

21
00:01:17,520 --> 00:01:20,550
So they actually have a syntax reference, a nice wiki.

22
00:01:20,550 --> 00:01:24,600
Some things are out of date, but it's easy to figure it out and I'll show you how it works here anyway.

23
00:01:25,110 --> 00:01:27,740
So I'm going to go back to my application and just take you through the code.

24
00:01:27,990 --> 00:01:29,430
So very simple app.

25
00:01:29,430 --> 00:01:36,270
I'm in my main package, which is in command Web main, and I just define some routes.

26
00:01:36,270 --> 00:01:39,570
And for this one, I used the I think I use Pat.

27
00:01:39,570 --> 00:01:40,890
Let's find the routes and find out.

28
00:01:41,010 --> 00:01:45,000
Yes, I use the pat the pat router, which I went through in this course already.

29
00:01:45,540 --> 00:01:50,640
And I just define two routes slash, which is the home page and send, which is just an empty page.

30
00:01:50,640 --> 00:01:51,780
I needed to put something there.

31
00:01:51,780 --> 00:01:56,070
So I put send so back to our main goal.

32
00:01:56,100 --> 00:02:01,380
Once we do this we actually listen and serve and we listen on Port AT&amp;T, as we've been doing in our

33
00:02:01,380 --> 00:02:01,880
course.

34
00:02:02,280 --> 00:02:06,150
So let's go look and see what some of these handlers, how they work.

35
00:02:06,270 --> 00:02:09,000
OK, so I'll go to the homepage and the home page.

36
00:02:09,600 --> 00:02:11,160
This is the handler for the home page.

37
00:02:11,160 --> 00:02:16,620
And just as it does in our course, it takes a response writer and a pointer to a request.

38
00:02:16,860 --> 00:02:20,730
And then I just built up some data, just dummy data to send to a template.

39
00:02:20,730 --> 00:02:22,950
Let me space this out a bit so you can see a little better.

40
00:02:24,180 --> 00:02:29,070
So I start by defining a jet versus map variable.

41
00:02:29,190 --> 00:02:33,890
OK, so I just make that and it holds anything I want.

42
00:02:34,230 --> 00:02:38,790
So once I've created this variable called data, I call in the builtin method.

43
00:02:38,790 --> 00:02:41,910
I call the builtin method set and I give it a name.

44
00:02:41,940 --> 00:02:46,140
This is what I'm going to refer to it in using my jet template.

45
00:02:46,710 --> 00:02:49,650
And I just store some value in there and I can put whatever I want in there.

46
00:02:49,650 --> 00:02:56,310
So initially I just said user ID is equal to one, then I set another one first for first name is equal

47
00:02:56,310 --> 00:03:01,170
to a string, so I put it into one, I put a string in, then I define a slice of strings, which are

48
00:03:01,170 --> 00:03:07,650
the days of the week, and I set a variable in my data called DSW four days of week and passed in that

49
00:03:07,650 --> 00:03:08,550
slice of strings.

50
00:03:09,360 --> 00:03:13,410
Then I called Render Page and I'm passing that three things.

51
00:03:13,560 --> 00:03:16,380
I'm passing it the response rate, which I need to write to.

52
00:03:16,680 --> 00:03:20,700
That's what's going to be how we render information to the end user and their web browser.

53
00:03:21,210 --> 00:03:24,240
The name of the template I want to load and then the data.

54
00:03:24,270 --> 00:03:28,050
Now, this render page is not part of the template package.

55
00:03:28,050 --> 00:03:31,470
Just to be clear, I'm just calling my own function, which is right down here.

56
00:03:32,160 --> 00:03:41,400
And all I do is I have those three arguments and I create I have a variable called views and I call

57
00:03:41,400 --> 00:03:42,060
get template.

58
00:03:42,060 --> 00:03:45,600
Now, view is way up here and that's a jetset.

59
00:03:45,600 --> 00:03:51,840
And this is what you have to have to start with to make sure that you can actually use the jet template

60
00:03:51,840 --> 00:03:52,200
engine.

61
00:03:52,200 --> 00:03:53,310
And it's really, really simple.

62
00:03:54,060 --> 00:03:57,840
So I say, first of all, there's an OS file system loader.

63
00:03:57,870 --> 00:04:01,530
Look at the root level of the application in a folder called HTML.

64
00:04:01,560 --> 00:04:08,760
And over here I have an HTML folder that has two files home and sent the second one I put in development

65
00:04:08,760 --> 00:04:13,800
mode because I want to be able to load changes to my template without starting or stopping and restarting

66
00:04:13,800 --> 00:04:17,430
the application, kind of like the cache variable we used in our system.

67
00:04:18,630 --> 00:04:21,510
So I have this variable views down here.

68
00:04:21,660 --> 00:04:27,900
I say view error is equal to or assign the value of views, get the template wherever it is.

69
00:04:28,080 --> 00:04:30,030
Now, I passed homecourt jet here.

70
00:04:30,030 --> 00:04:31,440
So it goes to the file system.

71
00:04:31,740 --> 00:04:37,230
It looks in the directory I specified, which I called HTML and it looks for a file called Homeport

72
00:04:37,290 --> 00:04:39,120
Jet, which is the argument I passed.

73
00:04:40,230 --> 00:04:42,120
So it can't get that.

74
00:04:42,300 --> 00:04:44,670
It just returns an error and logs the error.

75
00:04:44,910 --> 00:04:51,840
Otherwise I execute that view, that homecourt jet and I pass it my response writer, because it gives

76
00:04:51,840 --> 00:04:58,860
it something to write to the data, which is that jet set variable that I defined a little while ago

77
00:04:58,860 --> 00:04:59,910
and put a few values.

78
00:05:00,690 --> 00:05:02,520
And the third one is actually context.

79
00:05:02,550 --> 00:05:03,840
I'm not going to bother going through that.

80
00:05:03,840 --> 00:05:05,090
You can read it yourself.

81
00:05:05,100 --> 00:05:11,340
It's another place to store information and it uses the context to store that that executes that template

82
00:05:11,340 --> 00:05:13,350
if it can't execute it, if it turns an error.

83
00:05:13,410 --> 00:05:21,630
So let's start this application, go run command web main or start go try that again.

84
00:05:21,780 --> 00:05:26,360
Stargirl starts the application, listens on Port AT&amp;T.

85
00:05:26,370 --> 00:05:35,130
I go back to my Web browser, I go to a tab and I go to localhost AT&amp;T and I'm not going to put anything

86
00:05:35,130 --> 00:05:35,340
in there.

87
00:05:35,340 --> 00:05:37,680
I want to display the homepage and there it is.

88
00:05:37,680 --> 00:05:39,750
It displays at all very, very simple.

89
00:05:39,990 --> 00:05:43,980
Let's go look at the template and see how that how that is different from the built in go templates.

90
00:05:44,790 --> 00:05:45,690
It's actually pretty simple.

91
00:05:45,700 --> 00:05:49,570
So let me go to the homepage and hide this and have a look.

92
00:05:49,620 --> 00:05:51,990
So this is just a simple HTML page.

93
00:05:52,260 --> 00:05:58,230
I just load bootstraps success and bootstraps JavaScript, which I'm not even using, but I load it

94
00:05:58,560 --> 00:06:01,890
and then I just say this is the homepage and then user ID.

95
00:06:02,160 --> 00:06:05,240
I just refer to user ID with no dot.

96
00:06:05,280 --> 00:06:06,570
That's one difference right away.

97
00:06:07,320 --> 00:06:14,940
And the first name is first with no dot and ranging over things range key value is a the value of and

98
00:06:14,940 --> 00:06:15,980
that's pretty straightforward.

99
00:06:16,140 --> 00:06:20,940
Now the syntax is actually a little simpler than it is in the goal line templates and you have some

100
00:06:20,940 --> 00:06:22,140
built in functions as well.

101
00:06:22,140 --> 00:06:26,550
So let's go back and look at those cloudy kit will go to the wiki.

102
00:06:27,600 --> 00:06:29,360
Some things are out of date, but not too many.

103
00:06:29,460 --> 00:06:32,520
Someday I'm going to go rewrite his documentation and give it to him.

104
00:06:33,030 --> 00:06:38,670
So built in functions, for example, you get some nice simple built in functions to change things to

105
00:06:38,670 --> 00:06:44,880
lowercase, to uppercase, to trim space, check, to see if a variable is actually there or not, which

106
00:06:44,880 --> 00:06:47,610
a lot of people miss and go like templates, including me.

107
00:06:48,420 --> 00:06:50,760
And you get all of these other ones as well.

108
00:06:51,030 --> 00:06:54,000
And the documentation is really, really easy to work with.

109
00:06:54,000 --> 00:07:01,830
So, for example, let's see if we can find decisions, if then else here we are if expression and see

110
00:07:01,830 --> 00:07:03,030
how easy this is.

111
00:07:03,030 --> 00:07:10,350
And it's simpler than using the ECU or any or all of those little comparison operators that you have

112
00:07:10,350 --> 00:07:11,490
in the go line templates.

113
00:07:11,520 --> 00:07:16,890
Now, it sounds as though I'm suggesting you use jet templates instead of the built in go templates,

114
00:07:16,890 --> 00:07:18,780
and nothing could be further from the truth.

115
00:07:18,780 --> 00:07:25,590
I think it's really important that people coming to go for the first time become used to the built in

116
00:07:25,590 --> 00:07:28,740
templating syntax because you're going to run into it in other projects.

117
00:07:29,880 --> 00:07:34,050
But once you've mastered that or you're really comfortable with it, if you want to use something like

118
00:07:34,050 --> 00:07:36,090
Jet, there's nothing to stop you from doing that.

119
00:07:36,090 --> 00:07:37,590
And a lot of people find it easier.

120
00:07:38,070 --> 00:07:42,600
Also, templated inheritance is built in all those things that you really expect to find from a good

121
00:07:42,600 --> 00:07:43,500
templating package.

122
00:07:44,100 --> 00:07:45,210
All right, one other thing.

123
00:07:45,210 --> 00:07:46,350
I want to draw your attention.

124
00:07:46,440 --> 00:07:47,970
So this is my homepage.

125
00:07:48,600 --> 00:07:51,840
Here's one thing, one compelling reason why a lot of people come here.

126
00:07:51,840 --> 00:07:57,780
Let me define or refer to a variable ABC that I know doesn't exist, and I'll save that.

127
00:07:58,410 --> 00:07:59,970
And I'll go back to my.

128
00:08:01,250 --> 00:08:03,810
Web browser right here, find my home page.

129
00:08:03,860 --> 00:08:10,820
Let me reload this, and now it gives me a meaningful error message which is conspicuous by its absence

130
00:08:10,820 --> 00:08:13,060
in the built in go templating system.

131
00:08:13,700 --> 00:08:17,390
And this, if nothing else, is extremely helpful.

132
00:08:17,930 --> 00:08:18,950
So there you go.

133
00:08:18,960 --> 00:08:20,960
I just wanted to draw your attention to this.

134
00:08:20,960 --> 00:08:25,520
And if you really like jump chat templates and want to use those instead of the ones that I'm using

135
00:08:25,520 --> 00:08:28,630
for the remainder of this course, feel free to do so.

136
00:08:28,640 --> 00:08:34,970
But I would strongly encourage you to leave your experimentation with go templates to a separate project

137
00:08:35,270 --> 00:08:40,130
and follow along with the way that I'm doing it in this course or finish the course and then rewrite

138
00:08:40,130 --> 00:08:44,440
the pages in Jet, which is not a terribly onerous task.
