1
00:00:00,840 --> 00:00:06,270
So this time around, we want to work with layouts that allow us to simplify our templates, and if

2
00:00:06,270 --> 00:00:11,400
you look at your templates folder right now, we have to about which looks like this, let me hide the

3
00:00:11,400 --> 00:00:17,460
terminal at home, which looks almost exactly the same.

4
00:00:17,490 --> 00:00:21,360
And this information at the top and then this little bit at the bottom.

5
00:00:21,360 --> 00:00:27,010
These are the things we want to have existed in one place and not on every single page of our site.

6
00:00:27,030 --> 00:00:28,850
The rationale for this is pretty straightforward.

7
00:00:28,860 --> 00:00:34,970
If I need to make a change to the top of the site, like here, for example, I'm importing the bootstrap

8
00:00:34,970 --> 00:00:40,950
success if I want it to use some other success, I don't want to have to change it on every single page

9
00:00:40,950 --> 00:00:41,490
of the site.

10
00:00:41,490 --> 00:00:43,060
That's just extremely time consuming.

11
00:00:43,440 --> 00:00:49,020
So what we're going to do is we're going to create a new file in the templates folder and we're going

12
00:00:49,020 --> 00:00:49,830
to call that.

13
00:00:50,040 --> 00:00:50,940
It's just a file.

14
00:00:51,270 --> 00:00:54,960
We're going to call it base layout template.

15
00:00:55,560 --> 00:00:58,830
And it is important that you have this middle part layout.

16
00:00:58,890 --> 00:01:01,830
OK, I would actually use the temporal extension.

17
00:01:01,830 --> 00:01:06,720
You can use HTML or whatever you want to, but HTML is the standard, at least in my world.

18
00:01:06,840 --> 00:01:08,940
So I'm going to create that and it's a new empty file.

19
00:01:09,630 --> 00:01:12,960
Then I'm going to copy everything from the homepage.

20
00:01:12,990 --> 00:01:14,160
I'm going to copy all of it.

21
00:01:14,690 --> 00:01:20,390
I'm going to paste it in here and then I'm going to add a couple of things at the very top.

22
00:01:20,400 --> 00:01:27,600
I'm going to create a new blank line and I'm going to in double curly braces, I'm going to say define

23
00:01:27,870 --> 00:01:29,790
and then in quotes face.

24
00:01:30,560 --> 00:01:37,110
And then at the very bottom of this, right after the closing HTML tag, I'm going to put in double

25
00:01:37,110 --> 00:01:40,050
curly brackets and OK.

26
00:01:40,050 --> 00:01:43,920
And then just to make things format nicely, I will indent everything in between.

27
00:01:44,280 --> 00:01:49,260
All I've done here and this is something you're going to become very familiar with is I've used inside

28
00:01:49,260 --> 00:01:54,470
those double curly braces directives that are specific to go laying templates.

29
00:01:55,080 --> 00:01:58,470
So before these were added, this was just an HTML.

30
00:01:58,800 --> 00:02:00,120
There was nothing special about it all.

31
00:02:00,150 --> 00:02:05,580
It was passed as a goal line template where it looked for these double curly braces and things in between

32
00:02:05,580 --> 00:02:09,270
them to do things didn't find any, so it just served up the HTML5.

33
00:02:09,570 --> 00:02:15,390
And all I've done here is define everything between this tag, define base and this tag.

34
00:02:15,390 --> 00:02:17,850
And I've said everything in between.

35
00:02:17,850 --> 00:02:23,290
That is a as is defined with the name base and it's going to be a layer.

36
00:02:23,490 --> 00:02:26,570
OK, now I'm going to add some other things in here.

37
00:02:26,580 --> 00:02:31,020
Clearly, I don't want this is the home page on every page of the site.

38
00:02:31,200 --> 00:02:34,650
In fact, I'm going to delete everything between those body tags.

39
00:02:34,680 --> 00:02:37,740
I'm just going to delete it and I'm going instead.

40
00:02:37,740 --> 00:02:39,720
I'm going to put a little directive in there.

41
00:02:40,170 --> 00:02:42,510
And that directive is a block.

42
00:02:42,510 --> 00:02:43,770
A content block.

43
00:02:44,790 --> 00:02:46,650
Block is my directive.

44
00:02:47,010 --> 00:02:49,020
And the name is content.

45
00:02:49,320 --> 00:02:51,660
And this seems odd, but I'll explain it to you.

46
00:02:51,840 --> 00:02:53,550
I'm going to put a period after that.

47
00:02:54,150 --> 00:03:00,900
So there's an opening block tag and now I'll put a closing block tag, which is basically nothing more

48
00:03:00,900 --> 00:03:01,430
than the word.

49
00:03:01,440 --> 00:03:10,110
And so I've just defined a section in this layout that says to the goal line and Googling it says to

50
00:03:10,110 --> 00:03:10,620
go line.

51
00:03:11,670 --> 00:03:15,600
What will you see between this opening block and this envelope, this instatement?

52
00:03:15,930 --> 00:03:18,300
This will change on a per template basis.

53
00:03:18,300 --> 00:03:24,060
I might have content I want to stick in there and that content might be just HTML, in which case I

54
00:03:24,060 --> 00:03:26,430
wouldn't need this dot more often.

55
00:03:26,430 --> 00:03:33,360
It's going to include data that I pass from my handlers to the template renderer to render on the template.

56
00:03:33,360 --> 00:03:39,390
And all that DOT means is include whatever data you have with this template and send it to this block

57
00:03:39,390 --> 00:03:41,040
so we can use it if it has to.

58
00:03:42,240 --> 00:03:43,200
Now I define that.

59
00:03:43,200 --> 00:03:47,340
And just to make things simpler, because I'm going to use this later on, I'm going to find two more

60
00:03:47,340 --> 00:03:48,600
blocks up on the top.

61
00:03:48,900 --> 00:03:56,430
I'm going to find a new block, which I'll call success, and I will pass it data as well, and then

62
00:03:56,430 --> 00:03:57,570
I'll have an ending tag for it.

63
00:03:58,350 --> 00:04:05,370
And and then at the bottom, just before the closing body tag, I'm going to have another block called

64
00:04:05,370 --> 00:04:07,170
Jazz for JavaScript.

65
00:04:07,500 --> 00:04:09,280
And again, I need a closing tag.

66
00:04:09,570 --> 00:04:12,990
Now, the reason I'm doing this is that it's entirely possible.

67
00:04:12,990 --> 00:04:14,250
Block j ust dot.

68
00:04:14,340 --> 00:04:15,650
I want to pass it data as well.

69
00:04:16,590 --> 00:04:22,680
It's entirely possible, in fact, likely that I'm going to have some custom access for one page, but

70
00:04:22,680 --> 00:04:23,460
not for another.

71
00:04:23,460 --> 00:04:24,840
And this gives me a place to put it.

72
00:04:24,850 --> 00:04:26,480
That's the only reason it exists.

73
00:04:26,880 --> 00:04:32,460
It's also extremely likely that I'll have some JavaScript which is specific to some pages, but not

74
00:04:32,460 --> 00:04:34,350
others, and this gives me a place to put it.

75
00:04:35,010 --> 00:04:41,310
So all I've done here is to find a template, a layout for our templates that says here's the base template.

76
00:04:41,820 --> 00:04:47,880
Everything might have access entry, it might have a JavaScript entry and it might have a content entry.

77
00:04:48,240 --> 00:04:49,400
And I can say that.

78
00:04:49,410 --> 00:04:51,300
So now I've created this template.

79
00:04:51,630 --> 00:04:54,770
The obvious question is how do I use it?

80
00:04:55,080 --> 00:04:59,360
Well, to start with, let's go back and say simplify our homepage.

81
00:04:59,850 --> 00:05:00,330
Now this.

82
00:05:00,460 --> 00:05:05,710
Hard here from d'Hiv container and to the ending, Dave and all that stuff in between.

83
00:05:05,800 --> 00:05:08,500
That's the only content I want on this template.

84
00:05:08,530 --> 00:05:09,430
I don't want the rest of it.

85
00:05:09,430 --> 00:05:10,680
So I'm going to copy that, too.

86
00:05:10,700 --> 00:05:11,290
So I have it.

87
00:05:12,040 --> 00:05:14,560
And then I'm going to select all of this text and delete it.

88
00:05:14,830 --> 00:05:19,930
And I'm going to start this template with a directive, a go line template directive.

89
00:05:20,320 --> 00:05:22,510
And it's simply the word template.

90
00:05:22,540 --> 00:05:28,750
Use this template, use the template called base and include the data that's passed and then I'll have

91
00:05:28,750 --> 00:05:29,200
an end.

92
00:05:31,360 --> 00:05:33,020
Actually, you don't need an end for that one.

93
00:05:33,040 --> 00:05:34,840
I'm going to delete that just to say don't confuse you.

94
00:05:34,930 --> 00:05:36,680
This is a simple directive that's not closing.

95
00:05:36,700 --> 00:05:38,730
This just says it has no closing tag.

96
00:05:38,740 --> 00:05:41,800
It just says use the template named base.

97
00:05:41,800 --> 00:05:48,050
And this word right here, base corresponds to this word in base layout template.

98
00:05:49,360 --> 00:05:50,500
So now I have to find that.

99
00:05:50,860 --> 00:05:52,030
And when am I going to have it here?

100
00:05:52,060 --> 00:05:56,340
Well, I could have defined and I want to define content.

101
00:05:58,620 --> 00:06:04,110
And it has a closing time, and in between those, I'll post what I copied a little a few minutes ago.

102
00:06:04,620 --> 00:06:07,620
So now look how simple and short this template is.

103
00:06:07,650 --> 00:06:09,390
It says use the base template.

104
00:06:09,600 --> 00:06:14,160
And anywhere on that base template where you find a block name content put this content.

105
00:06:14,970 --> 00:06:20,180
And the fact that I have the dot here means that my data is available for the entire template.

106
00:06:21,510 --> 00:06:23,430
So that's all that I have to do for that.

107
00:06:23,430 --> 00:06:27,570
And I can do exactly the same thing for base layout or so for about Page.

108
00:06:27,580 --> 00:06:31,290
So I come in here, I find the things that I want to be specific to this.

109
00:06:31,290 --> 00:06:41,970
I cut it, delete everything, say use the template base and pass the data to it and then define content.

110
00:06:45,080 --> 00:06:54,330
Paste in my HTML specific to the Web page and close that define tag, and now that is a lot less HTML

111
00:06:54,330 --> 00:06:56,200
to maintain on a per page basis.

112
00:06:56,460 --> 00:07:01,400
So I defined this base layout, I defined the home page and define the about page.

113
00:07:01,590 --> 00:07:09,600
But if I run the application right now, which I'm going to so go run command web Stargirl, there's

114
00:07:09,600 --> 00:07:13,770
only one file so I could put Mango, but I like to get the habit of going Stargirl because I know him

115
00:07:13,770 --> 00:07:15,870
and have more files in there later and run it.

116
00:07:18,340 --> 00:07:24,730
Then switch to my favorite Web browser, open up a browser window and go to local eighty.

117
00:07:26,020 --> 00:07:27,740
Well, I didn't seem to work very well.

118
00:07:27,940 --> 00:07:28,650
What's in there?

119
00:07:28,690 --> 00:07:34,080
If I view the source on this view the while, I'll just go view source.

120
00:07:35,500 --> 00:07:36,490
There's nothing in there.

121
00:07:36,520 --> 00:07:37,220
It's empty.

122
00:07:37,270 --> 00:07:37,810
Why?

123
00:07:37,930 --> 00:07:43,470
Well, because although I've told this template for the homepage, so let's find home again.

124
00:07:44,140 --> 00:07:46,750
I've said use the template base.

125
00:07:46,750 --> 00:07:48,280
Well, I can't find the template base.

126
00:07:48,300 --> 00:07:54,070
And if I look down here, the error messages in the console in my terminal, it says error parsing,

127
00:07:54,070 --> 00:07:59,380
template, template, homepage, template, no such template base.

128
00:07:59,680 --> 00:08:07,210
I need to actually tell go how to render this template that I'm using layouts and that fortunately is

129
00:08:07,210 --> 00:08:08,630
not all that difficult.

130
00:08:09,040 --> 00:08:10,900
So how are we going to do that?

131
00:08:10,930 --> 00:08:14,190
Well, we already know we have a package here called Render.

132
00:08:14,230 --> 00:08:14,550
Right.

133
00:08:15,190 --> 00:08:18,010
And inside of that is where I actually render the template.

134
00:08:18,970 --> 00:08:22,030
So name starts with package name, and that's OK, I don't mind that.

135
00:08:23,420 --> 00:08:27,320
How do I actually make this thing do what I want it to do?

136
00:08:27,590 --> 00:08:29,770
Well, let's think about this for a minute.

137
00:08:30,440 --> 00:08:35,690
So what I need to do is something more sophisticated than simply saying go to the templates directory

138
00:08:35,690 --> 00:08:38,790
right here, find the template with that name and render it.

139
00:08:39,020 --> 00:08:46,730
I actually need to somehow tell it, find all of the based on all of the pages in this folder, whatever

140
00:08:46,730 --> 00:08:53,810
they may be, find the layouts associated with them and combine those into a template that I can actually

141
00:08:53,810 --> 00:08:54,170
serve.

142
00:08:54,350 --> 00:08:54,790
All right.

143
00:08:54,920 --> 00:09:03,770
So what I'm going to do is I'm going to create a new function and I'll call it a render template test

144
00:09:03,890 --> 00:09:06,770
just so I don't overwrite what I have already.

145
00:09:06,950 --> 00:09:13,270
And I'll give it now in my response writer and give it my tampoe string.

146
00:09:13,280 --> 00:09:14,510
So the same arguments.

147
00:09:15,080 --> 00:09:17,600
And I'm going to do things a little bit differently to start with.

148
00:09:17,840 --> 00:09:20,840
I'm going to create something that you may not be familiar with.

149
00:09:21,140 --> 00:09:23,420
It is a go langue map.

150
00:09:23,420 --> 00:09:24,770
It's just a data structure.

151
00:09:24,770 --> 00:09:28,880
Some people call these associative arrays and that would be the closest corresponding thing.

152
00:09:28,880 --> 00:09:35,060
And this is just a data structure that holds whatever I tell it to hold so I can look up things very,

153
00:09:35,060 --> 00:09:35,690
very quickly.

154
00:09:36,140 --> 00:09:39,290
So I'm going to do that by creating something I'm going to call a cache.

155
00:09:39,290 --> 00:09:45,110
When I'm going to create is a template cache that holds all of my templates and it creates it at the

156
00:09:45,110 --> 00:09:46,190
start of the application.

157
00:09:46,700 --> 00:09:54,530
So I create a variable, my cache, and I'm going to assign that the type of map string.

158
00:09:56,750 --> 00:10:02,030
And that I'm going to put in a point or two template, dot template.

159
00:10:03,060 --> 00:10:08,630
And I need to close that with opening and closing curly brackets, which is pretty straightforward.

160
00:10:08,670 --> 00:10:12,060
OK, so that is something that I've just created.

161
00:10:12,060 --> 00:10:15,210
My cache is a map string template template.

162
00:10:17,140 --> 00:10:18,550
All right, so what am I going to do with that?

163
00:10:19,210 --> 00:10:26,710
Well, the first thing I need to do is to tell it you need to go and get all of the files at a particular

164
00:10:26,710 --> 00:10:27,280
location.

165
00:10:27,550 --> 00:10:30,190
OK, so how do I tell you to do that?

166
00:10:30,220 --> 00:10:32,000
Well, first of all, what am I going to get?

167
00:10:32,020 --> 00:10:37,720
I'm going to go get all of the pages, all of the things in this template folder that actually have

168
00:10:37,720 --> 00:10:39,180
the name page in there.

169
00:10:39,190 --> 00:10:42,970
So before the template, I have a I have a page.

170
00:10:44,170 --> 00:10:46,060
Let's go get those so pages.

171
00:10:46,060 --> 00:10:48,250
I'm going to go get those and error.

172
00:10:48,250 --> 00:10:52,180
I'm going to be checking for errors is going to be assigned.

173
00:10:52,750 --> 00:11:00,790
And this is a built in function found in go file path dot Glaube strange name galore, but it actually

174
00:11:00,790 --> 00:11:02,200
kind of makes sense to me to think it through.

175
00:11:02,500 --> 00:11:03,670
And I want to find.

176
00:11:05,040 --> 00:11:13,680
Everything that exists in dot slash templates and then star dot page, dot tempo.

177
00:11:14,670 --> 00:11:20,190
Now all that really does is say go to the templates folder, find everything the templates folder that's

178
00:11:20,190 --> 00:11:26,070
at the root level of my application and get all of the files that start with anything but definitely

179
00:11:26,070 --> 00:11:28,410
end with dot page, dot tempo.

180
00:11:28,440 --> 00:11:35,460
So this is going to select about Dot Page, Dot Temple and home page Tapio and then I'll check for errors.

181
00:11:35,490 --> 00:11:40,910
OK, so if error is not equal to nil then I want to do what.

182
00:11:40,960 --> 00:11:42,930
Well what do I want to do here.

183
00:11:43,170 --> 00:11:44,100
Let's return an error.

184
00:11:44,970 --> 00:11:47,370
So return an error.

185
00:11:47,370 --> 00:11:50,760
Which means up here I have to say it returns the type of error.

186
00:11:51,150 --> 00:11:51,540
OK.

187
00:11:51,630 --> 00:11:54,840
And it's not called er it's called e r r try that again.

188
00:11:55,860 --> 00:11:57,830
Erm all right.

189
00:11:57,840 --> 00:12:02,300
So now I've got my pages, I haven't done anything with them yet so I've got to do something with it.

190
00:12:02,490 --> 00:12:05,720
So now I'm going to use another control structure that you haven't seen yet.

191
00:12:05,730 --> 00:12:09,150
You may have seen it if you've done some work at other programming languages but you haven't seen it

192
00:12:09,150 --> 00:12:09,720
in go.

193
00:12:10,440 --> 00:12:16,230
It's a for loop and the way that for loops working go is really straightforward for and then the first

194
00:12:16,230 --> 00:12:18,480
thing would be index, which I'm going to get rid of in a minute.

195
00:12:18,480 --> 00:12:21,630
But this is for index and then page.

196
00:12:21,630 --> 00:12:23,430
That's just a name that I'm giving.

197
00:12:23,430 --> 00:12:27,600
It is a signed range through pages and then I do something.

198
00:12:28,440 --> 00:12:30,660
So I have two things that are happening here.

199
00:12:30,840 --> 00:12:36,570
For every page that I find, I am going to get the index and the first time through will be zero.

200
00:12:37,050 --> 00:12:40,020
And then then I'm going to get the actual page itself.

201
00:12:40,020 --> 00:12:46,680
Will the page that I get here is a collection or a selection of all the files that is found in this

202
00:12:46,680 --> 00:12:47,610
variable pages.

203
00:12:47,880 --> 00:12:51,840
So it might find about first, therefore I will equal zero.

204
00:12:51,840 --> 00:12:58,980
The first iteration and page will equal about Page Tampoe and then the second time through this will

205
00:12:58,980 --> 00:13:04,020
go to one because we start counting at zero and four loops and the second one and the second value would

206
00:13:04,020 --> 00:13:05,910
be home page temple.

207
00:13:06,060 --> 00:13:11,660
It will not include based on layout because well, it doesn't end in page two.

208
00:13:12,810 --> 00:13:16,110
Now when I do this, what I what am I going to do when I start looping through this?

209
00:13:16,110 --> 00:13:17,400
Well, I've got this information.

210
00:13:17,400 --> 00:13:23,970
The first thing you get is the actual base name of the page, because what it's returning here is actually

211
00:13:23,970 --> 00:13:29,250
not just about page that template, it's turning the full path to that.

212
00:13:29,250 --> 00:13:31,410
And what I want, it's returning the full path to that.

213
00:13:31,410 --> 00:13:33,360
And what I want is the actual name of the page.

214
00:13:33,360 --> 00:13:39,530
And I can get that by saying name is assigned file path, not base page.

215
00:13:40,110 --> 00:13:40,580
All right.

216
00:13:40,590 --> 00:13:43,860
So now I've used this page variable and it's used here.

217
00:13:44,100 --> 00:13:48,920
And from that I've extracted the actual name, which is just about page template.

218
00:13:49,200 --> 00:13:50,280
I don't want the index.

219
00:13:50,280 --> 00:13:50,940
I'm not going to use it.

220
00:13:50,940 --> 00:13:53,520
So I'm actually going to say don't do anything with the index.

221
00:13:53,850 --> 00:13:56,890
Now that I have this name, what do I want to do with it?

222
00:13:57,600 --> 00:14:02,070
Well, I'm going to create a template set and a template set.

223
00:14:02,070 --> 00:14:03,780
It sounds ever so complicated.

224
00:14:03,780 --> 00:14:08,550
It's not all I'm going to do is give it create a new variable test for template set.

225
00:14:08,970 --> 00:14:16,050
And I'm going to check for an error that's assigned the value of template, not new.

226
00:14:17,730 --> 00:14:24,690
And the name I want to use for that template is the actual name I just extracted name and then I want

227
00:14:24,690 --> 00:14:25,770
to give it Funk's.

228
00:14:26,310 --> 00:14:31,320
This template might have some functions and I'm going to give it a word functions which we haven't defined

229
00:14:31,320 --> 00:14:31,470
yet.

230
00:14:31,470 --> 00:14:32,400
But don't worry about that.

231
00:14:32,880 --> 00:14:35,340
And I want to pass files page.

232
00:14:35,350 --> 00:14:38,760
Wow, that's a lot of text, but what is it actually doing?

233
00:14:38,770 --> 00:14:40,590
Well, first of all, let me just do this.

234
00:14:41,190 --> 00:14:44,730
Copy this error check and paste it here.

235
00:14:44,910 --> 00:14:46,770
OK, what's this function.

236
00:14:46,770 --> 00:14:51,030
Well, we haven't seen that what what that actually is yet, but it is something that I am going to

237
00:14:51,030 --> 00:14:51,510
use.

238
00:14:51,510 --> 00:14:57,720
OK, what I'm going to do up here is outside of this function and outside of that function, just out

239
00:14:57,720 --> 00:15:06,720
to the input statement that I'm going to create a new function, OK, a new variable, I'm going to

240
00:15:06,720 --> 00:15:07,680
call it functions.

241
00:15:08,190 --> 00:15:11,520
And all it's going to do, it's equal to template dot.

242
00:15:11,520 --> 00:15:13,890
Func Map will be seeing this shortly.

243
00:15:13,890 --> 00:15:19,140
I'll explain it in a moment, but let's get it typed out first and it's going to be empty, OK, it

244
00:15:19,140 --> 00:15:19,560
has nothing.

245
00:15:20,190 --> 00:15:23,010
So I've created a variable called functions.

246
00:15:23,160 --> 00:15:24,630
It's a type template.

247
00:15:24,630 --> 00:15:25,470
Func map.

248
00:15:25,650 --> 00:15:27,200
What does this do?

249
00:15:27,210 --> 00:15:32,430
Well, I think map is nothing more than a map of functions that I can use in a template.

250
00:15:32,430 --> 00:15:38,070
I am going to, at some point in this course, need to do something in a go line template that is not

251
00:15:38,070 --> 00:15:39,210
built into the language.

252
00:15:39,330 --> 00:15:46,230
Maybe I want to format a date, maybe I want to return the current year, whatever it may be.

253
00:15:46,440 --> 00:15:52,770
It's not built into the templating language, but go allows us to create our own functions and pass

254
00:15:52,770 --> 00:15:53,880
those to the template.

255
00:15:54,030 --> 00:15:55,920
We don't have any yet, but we'll get some later.

256
00:15:55,920 --> 00:15:58,080
Let's just set it up right now because we know we're going to use it.

257
00:15:58,710 --> 00:16:02,100
So now I've created my template set based upon this page.

258
00:16:02,100 --> 00:16:04,110
I've passed an empty variable function.

259
00:16:04,530 --> 00:16:07,450
That will eventually have some functions in there that we can use.

260
00:16:07,770 --> 00:16:10,680
I've checked for errors, so, so far so good.

261
00:16:11,130 --> 00:16:12,330
What happens next?

262
00:16:13,500 --> 00:16:18,720
Well, first of all, we need to find out, does this template match any layouts?

263
00:16:18,720 --> 00:16:24,750
In other words, should I use a layout that's defined for this template with this particular template?

264
00:16:24,930 --> 00:16:28,420
OK, so we're going to check and see if it matches something.

265
00:16:29,130 --> 00:16:33,930
So let's look for the existence of any layouts in this particular folder called templates.

266
00:16:33,930 --> 00:16:35,700
And we can do that by saying matches.

267
00:16:35,950 --> 00:16:37,310
That's a variable that I'm picking.

268
00:16:37,320 --> 00:16:41,330
And I just like it because actually we're checking to see if something matches again.

269
00:16:41,340 --> 00:16:42,510
We'll check for an error.

270
00:16:42,840 --> 00:16:49,710
And that's going to be assigned the value of, once again, this glob thing, file path globe.

271
00:16:51,150 --> 00:17:00,880
And we're going to look in the folder, dot slash, star layout star, dot layout, dot temple.

272
00:17:01,890 --> 00:17:07,980
So that's going to say look for anything, any file in that directory specified here, which is actually

273
00:17:07,980 --> 00:17:10,330
supposed to be dot templates.

274
00:17:10,350 --> 00:17:11,070
Try that again.

275
00:17:11,670 --> 00:17:17,430
Templates start looking the templates directory for any file that ends and layout HTML.

276
00:17:17,910 --> 00:17:20,510
And if you can find one, we're going to do something with it.

277
00:17:20,700 --> 00:17:24,510
But first, let's check for errors to make sure that everything works the way it's supposed to, because

278
00:17:24,510 --> 00:17:28,470
maybe I had a typo in here where I left the letter out and I can't find the necessary directory, but

279
00:17:28,470 --> 00:17:33,720
whatever it's good practice to check for errors, then we're going to look at this variable matches.

280
00:17:34,140 --> 00:17:40,740
If there's at least one thing that it finds in there, then the length of matches will be greater than

281
00:17:40,740 --> 00:17:41,120
zero.

282
00:17:41,190 --> 00:17:48,390
So I can say if Len, which is a built in function go matches, is greater than zero, then I want to

283
00:17:48,390 --> 00:17:48,920
do something.

284
00:17:49,230 --> 00:17:50,830
And what do I want to do?

285
00:17:51,270 --> 00:18:00,540
Well, I want to actually again go to my template set, which I've already created, check for an error

286
00:18:01,470 --> 00:18:06,750
and I want to pass that Glaube so pass Glaube.

287
00:18:09,220 --> 00:18:13,060
And it's going to be, once again, just the path to the templates.

288
00:18:14,250 --> 00:18:18,000
Templates start up template, sort of layout.

289
00:18:20,500 --> 00:18:21,280
Tampoe.

290
00:18:24,480 --> 00:18:30,800
We're going to pass it will check for errors once again, if there's an error, just return the error.

291
00:18:31,890 --> 00:18:33,110
So we've passed it now.

292
00:18:33,660 --> 00:18:35,100
And what do we do with it next?

293
00:18:35,130 --> 00:18:37,750
Well, we're all set off right now for that part.

294
00:18:37,770 --> 00:18:38,490
For that part.

295
00:18:38,850 --> 00:18:44,820
All we need to do at this point is to take that template set that we've just spent all this time creating.

296
00:18:45,150 --> 00:18:48,150
And we need to add it to the cash that we created up here.

297
00:18:48,160 --> 00:18:50,460
We're finally going to use this map we created.

298
00:18:51,150 --> 00:18:52,820
And the way that it works is very simple.

299
00:18:52,950 --> 00:19:00,350
My cash name, which is the actual name of the template, equals the template set I just created.

300
00:19:01,020 --> 00:19:01,680
So I've got that.

301
00:19:01,680 --> 00:19:02,400
I've created it.

302
00:19:03,000 --> 00:19:05,750
And now because I have a function here, return no.

303
00:19:06,780 --> 00:19:13,050
So we've gone to all this trouble of creating render template test, which is going to become rendered

304
00:19:13,050 --> 00:19:13,980
template in just a minute.

305
00:19:14,550 --> 00:19:15,680
And let's go through this again.

306
00:19:15,690 --> 00:19:21,360
What I've done is create a map and a map is just something I can look up very look up a value in very

307
00:19:21,360 --> 00:19:21,800
quickly.

308
00:19:22,020 --> 00:19:26,040
This happens to have a string as its first parameter, the map index.

309
00:19:26,550 --> 00:19:30,190
And I'm using for the index the name of the template in question.

310
00:19:30,210 --> 00:19:34,500
So when I run this, my cache will actually have two entries.

311
00:19:35,010 --> 00:19:43,380
The first one is called about Page Tampoe and the second one is called Home Page Template.

312
00:19:43,560 --> 00:19:45,180
So that's how I can look things up.

313
00:19:45,570 --> 00:19:50,580
And when I look it up, it's going to give me a fully parsed and ready to use template.

314
00:19:50,700 --> 00:19:54,360
So and it's actually a pointer to a template, but same idea for our purposes.

315
00:19:55,470 --> 00:20:01,230
When I need to render a template, I need to have access to this cache and I need to be able to do that

316
00:20:01,710 --> 00:20:02,370
in some way.

317
00:20:02,370 --> 00:20:03,480
That kind of makes sense.

318
00:20:03,510 --> 00:20:08,910
Now, if you look at the way I've built this variable right now, I've actually got this scoped.

319
00:20:08,910 --> 00:20:13,320
So it only exists within this particular function.

320
00:20:13,320 --> 00:20:14,420
And that's not very helpful.

321
00:20:14,670 --> 00:20:16,720
So maybe I should return that as well.

322
00:20:16,740 --> 00:20:19,740
So let me put brackets around this and return to values.

323
00:20:20,970 --> 00:20:25,800
And the first value of their return is exactly what I have right here, a map, string, template,

324
00:20:25,800 --> 00:20:26,280
template.

325
00:20:26,280 --> 00:20:27,090
So let's get that.

326
00:20:29,450 --> 00:20:30,440
And pasted in there.

327
00:20:32,290 --> 00:20:36,490
And we don't need this after, of course, because we're not actually this is how we're referring to

328
00:20:36,490 --> 00:20:36,940
the type.

329
00:20:36,940 --> 00:20:41,950
This is just a type, it's a map with an index of string and the contents are going to be a pointer

330
00:20:41,950 --> 00:20:43,150
to template template.

331
00:20:43,540 --> 00:20:44,830
And we're returning error.

332
00:20:45,460 --> 00:20:50,860
And here we can just return that before the error.

333
00:20:50,860 --> 00:20:53,380
So we'll compile and return that before the error.

334
00:20:53,380 --> 00:20:57,010
So we'll compile return that before the error one more time.

335
00:20:59,830 --> 00:21:04,480
And down here, if everything works the way that it's supposed to, I can now return this cash.

336
00:21:04,510 --> 00:21:04,950
All right.

337
00:21:05,380 --> 00:21:06,490
So we've got this function.

338
00:21:06,490 --> 00:21:07,210
It's all set.

339
00:21:07,210 --> 00:21:10,920
It's going to go through and pass our templates and return something.

340
00:21:11,980 --> 00:21:14,800
Well, let's test it and make sure it actually works.

341
00:21:14,830 --> 00:21:20,740
So what I'm going to do for just the sake of argument to make sure this works is I am going to every

342
00:21:20,740 --> 00:21:23,670
time I render a template, I'm going to create that template set.

343
00:21:24,040 --> 00:21:35,230
So put in here a template cache, I'll call it template cache and all error is assigned render template

344
00:21:35,230 --> 00:21:35,740
test.

345
00:21:36,100 --> 00:21:37,450
And that takes arguments.

346
00:21:37,450 --> 00:21:42,520
It takes a W and it takes uh, we don't even need the template string.

347
00:21:42,520 --> 00:21:43,270
Let's get rid of that.

348
00:21:47,960 --> 00:21:50,010
Let's just leave it right now.

349
00:21:50,030 --> 00:21:54,560
We're not using the writer at the moment, but we will later on and we'll just check for errors.

350
00:21:54,560 --> 00:21:57,020
So I'm going to copy an error check down here.

351
00:21:57,820 --> 00:21:58,550
Just type it out.

352
00:21:58,580 --> 00:22:08,510
It's not going to take that long if error is not equal to nil format, print line error, getting template

353
00:22:08,510 --> 00:22:11,300
cache and the error.

354
00:22:12,010 --> 00:22:12,490
All right.

355
00:22:12,620 --> 00:22:13,460
So that's it.

356
00:22:14,360 --> 00:22:19,210
And if you're not doing anything with that right now, so I better just do this instead.

357
00:22:19,970 --> 00:22:21,250
They're all right.

358
00:22:21,470 --> 00:22:25,090
And now, because we have an error in two places, I don't need to initialize it twice.

359
00:22:25,730 --> 00:22:33,620
So in theory, if I actually run this code, just hit a page on the Web browser, it's going to execute

360
00:22:33,620 --> 00:22:33,890
this.

361
00:22:33,890 --> 00:22:35,580
But I want to make sure it's actually doing it.

362
00:22:35,600 --> 00:22:46,380
So what I'm going to do here is put another format, dot print line page is currently on the page.

363
00:22:46,430 --> 00:22:51,140
So what's going to happen when I every time I hit a page on the website, it's going to call render

364
00:22:51,150 --> 00:22:55,640
template to render it that it's going to execute this code, which I just have here, just to demonstrate

365
00:22:55,640 --> 00:22:58,550
that it's actually building a template cache or finding out if it's not.

366
00:22:59,360 --> 00:23:03,890
And every time that you render a page, this function gets called down here.

367
00:23:03,980 --> 00:23:05,960
It will create our map.

368
00:23:06,290 --> 00:23:12,440
It will go through the find all the necessary pages in template, in the templates folder, and then

369
00:23:12,440 --> 00:23:17,090
it will loop through that range, arrange for those pages and print out the name of the current page,

370
00:23:17,210 --> 00:23:22,070
and that will return a template cache if there are no errors, which we don't use at the moment.

371
00:23:22,070 --> 00:23:23,630
But let's try this out and see if it works.

372
00:23:23,690 --> 00:23:26,360
OK, so let's compile it.

373
00:23:29,500 --> 00:23:35,410
Good, and I'll hit enter a few times just to get blank lines, go back to this page, reload that page

374
00:23:36,010 --> 00:23:36,870
and come back here.

375
00:23:37,180 --> 00:23:40,600
So it said, look at the error passing template.

376
00:23:40,600 --> 00:23:41,830
That's the error we saw last time.

377
00:23:41,830 --> 00:23:42,790
So we can ignore that.

378
00:23:43,150 --> 00:23:48,190
But it actually found the about page and it found the the home page.

379
00:23:48,490 --> 00:23:49,950
So that's exactly what I need.

380
00:23:50,200 --> 00:23:50,700
Perfect.

381
00:23:51,250 --> 00:23:52,660
So let's go back to our code here.

382
00:23:53,950 --> 00:23:56,470
Stop this down or shut this down.

383
00:23:56,500 --> 00:24:01,270
So I'll stop the application and think about what we're going to do next.

384
00:24:01,280 --> 00:24:07,690
What I need to do is somehow to get my cash and use that to render a template.

385
00:24:08,230 --> 00:24:08,580
Hmm.

386
00:24:09,100 --> 00:24:10,000
How am I going to do that?

387
00:24:10,450 --> 00:24:12,150
I think this is long enough for one lecture.

388
00:24:12,160 --> 00:24:18,130
Let's break this one up into two and in the next one will actually make our template, set our cache

389
00:24:18,130 --> 00:24:24,430
of templates, make that usable and show pages using that nice layout we created at the start of this

390
00:24:24,430 --> 00:24:24,730
lecture.

391
00:24:24,760 --> 00:24:25,090
All right.

392
00:24:25,090 --> 00:24:27,670
I'll see you in the next one and we'll actually make this thing work.
