1
00:00:01,170 --> 00:00:07,680
So we're finally at the point where we can send some data from our handlers to go line templates, and

2
00:00:07,680 --> 00:00:09,330
that's not a very difficult thing to do.

3
00:00:09,360 --> 00:00:11,840
It took us a while to get here, but we're finally going to do it.

4
00:00:12,090 --> 00:00:13,860
So let's think about how that happens.

5
00:00:14,630 --> 00:00:15,720
I have these handlers.

6
00:00:15,720 --> 00:00:18,810
I mean, handlers, Dutko, I have I have two handlers right now.

7
00:00:18,810 --> 00:00:24,330
One called home and one called about and inside those handlers right now, all I'm doing is rendering

8
00:00:24,330 --> 00:00:24,870
a template.

9
00:00:24,870 --> 00:00:26,220
I'm not doing anything with them.

10
00:00:26,340 --> 00:00:29,250
I'm just reading a template from disk storing in the template cache.

11
00:00:29,700 --> 00:00:33,930
And every time the appropriate handler is called, I serve the appropriate template.

12
00:00:34,470 --> 00:00:35,330
And that's pretty good.

13
00:00:35,610 --> 00:00:37,680
But there are a lot of situations.

14
00:00:37,680 --> 00:00:39,660
For example, let's have a look at the about page.

15
00:00:40,200 --> 00:00:50,370
I might perform some business logic here, which gives me some data and then I need to send the data

16
00:00:50,370 --> 00:00:51,300
to the template.

17
00:00:51,870 --> 00:00:53,420
I don't have anywhere to do that right now.

18
00:00:53,460 --> 00:00:57,280
I have this function render template and I want to send it some data.

19
00:00:57,810 --> 00:01:03,120
So if you actually look at the render template function, it only has two arguments the response rate,

20
00:01:03,220 --> 00:01:08,190
which it has to have so it can actually have the somewhere to send the the finished webpage, the name

21
00:01:08,190 --> 00:01:11,580
of the template, but there's nowhere to actually pass data to it.

22
00:01:11,760 --> 00:01:17,880
If I had some data here, if I had a third parameter, I would actually be passing it right here on

23
00:01:17,880 --> 00:01:21,300
the part where I execute those bytes stored in a bytes buffer.

24
00:01:22,080 --> 00:01:27,870
I would pass the data in there and the execute function would take that data and put it in the correct

25
00:01:27,870 --> 00:01:29,510
place in the actual template.

26
00:01:29,910 --> 00:01:34,800
So clearly I need something to put here, but what kind of data am I going to be sending?

27
00:01:34,800 --> 00:01:35,850
It might be a date.

28
00:01:35,850 --> 00:01:37,140
It might be a string.

29
00:01:37,290 --> 00:01:40,560
It could be a full object representing a user.

30
00:01:40,680 --> 00:01:41,610
It could be anything.

31
00:01:41,610 --> 00:01:43,070
I have no idea what it's going to be.

32
00:01:43,620 --> 00:01:45,270
So what am I going to put here?

33
00:01:45,270 --> 00:01:51,150
I can't just say, well, I might have seventy five different kinds of data, therefore I'll have seventy

34
00:01:51,150 --> 00:01:58,580
five parameters to passing this in as parameters to this render template function that's really inefficient.

35
00:01:58,800 --> 00:02:03,710
Instead, let's create a new type, a new type that's going to hold template data.

36
00:02:04,320 --> 00:02:12,630
So right here back at handlers, I'm going to create at the top a new type and I shall call that type

37
00:02:13,230 --> 00:02:14,460
template data.

38
00:02:17,370 --> 00:02:22,320
And it's going to be a struct and it's going to hold whatever kinds of data I think I might need to

39
00:02:22,320 --> 00:02:22,800
put in there.

40
00:02:23,340 --> 00:02:25,950
So what kinds of things am I going to be sending?

41
00:02:26,220 --> 00:02:31,470
I'm definitely going to be sending string information because I'm going to probably send names and email

42
00:02:31,470 --> 00:02:33,570
addresses and all sorts of things that are strings.

43
00:02:33,870 --> 00:02:35,250
But how many might I send?

44
00:02:35,430 --> 00:02:37,290
I might send one for one template.

45
00:02:37,290 --> 00:02:39,290
I might send 20 for another template.

46
00:02:39,600 --> 00:02:45,690
So instead of just putting in a string type, let's put in a string that I'm going to define a type

47
00:02:45,690 --> 00:02:50,070
called string map, and it's going to be nothing more than a map of string string.

48
00:02:50,730 --> 00:02:53,280
So that's one thing I might be selling to templates.

49
00:02:53,730 --> 00:02:56,700
Another thing is I might be sending numbers into integers.

50
00:02:56,700 --> 00:03:00,870
So let's put an intermap in Intermap Map String.

51
00:03:04,360 --> 00:03:10,480
And then I might be sending floats, so let's put in a float bap float map, which will be a map of

52
00:03:10,480 --> 00:03:13,980
string to look things up and of type float.

53
00:03:13,990 --> 00:03:15,190
I'll just use float thirty two.

54
00:03:15,340 --> 00:03:18,130
There are 232 floats, float 32 and float sixty four.

55
00:03:18,130 --> 00:03:21,380
But we're not going to be using huge numbers of float 32 will do the trick.

56
00:03:22,060 --> 00:03:23,230
What else might I send.

57
00:03:23,560 --> 00:03:27,780
Well I might send things that are not maps, not strings, not floats.

58
00:03:27,790 --> 00:03:31,840
In fact they might be structures, they might be things that hold all kinds of other data.

59
00:03:32,350 --> 00:03:36,550
So for that one, I'm going to have a special type, which I will call simply data.

60
00:03:37,180 --> 00:03:41,200
And it's again, going to be a map and it will be a map with an index of string.

61
00:03:41,590 --> 00:03:44,530
But the actual content of that map can be anything.

62
00:03:44,710 --> 00:03:50,260
And in go when you're not sure what the data type is, you call it an interface.

63
00:03:50,260 --> 00:03:53,830
And because I'm declaring it as a type here, I have to put the curly brackets after it.

64
00:03:55,450 --> 00:03:59,590
So that's definitely something I'm going to possibly have to send and then I'm going to put some other

65
00:03:59,590 --> 00:04:03,610
ones in here just for the sake of doing them right now.

66
00:04:03,610 --> 00:04:04,720
So I don't have to do it later.

67
00:04:05,020 --> 00:04:11,260
When we get to the forms part, when we learn how to construct and post forms and handle the forum post

68
00:04:11,260 --> 00:04:17,230
in the handlers, we're actually going to have a little security token called a cross site request forgery

69
00:04:17,230 --> 00:04:17,620
token.

70
00:04:17,980 --> 00:04:19,060
We're not going to use it right now.

71
00:04:19,060 --> 00:04:24,130
And it's just a string, but it's something I want available on every page because a page might have

72
00:04:24,130 --> 00:04:25,060
a form at some point.

73
00:04:25,360 --> 00:04:26,490
So I'll declare it right now.

74
00:04:26,920 --> 00:04:29,950
RF token and it's just a string.

75
00:04:30,910 --> 00:04:34,450
And I'm also going to have some messages I send to the user.

76
00:04:34,780 --> 00:04:36,850
Some will be a success message.

77
00:04:36,850 --> 00:04:37,210
Yes.

78
00:04:37,210 --> 00:04:39,430
Your your submission to the site was successful.

79
00:04:39,430 --> 00:04:40,810
Some will be an error message.

80
00:04:40,810 --> 00:04:41,170
Oh no.

81
00:04:41,170 --> 00:04:45,880
You left an important piece out of the form and have to do it over again and some might just be a warming

82
00:04:45,910 --> 00:04:46,360
warning.

83
00:04:46,720 --> 00:04:48,580
So I'll set those up now as well.

84
00:04:48,610 --> 00:04:51,820
Flash is going to be a string.

85
00:04:52,510 --> 00:04:56,920
That's a flash flash message when I'm just posting on message, a success message or whatever to the

86
00:04:56,920 --> 00:05:03,250
end user warning will be a string and form will be a anot form.

87
00:05:04,270 --> 00:05:06,340
Error will also be a string.

88
00:05:06,640 --> 00:05:07,060
All right.

89
00:05:07,540 --> 00:05:10,470
Now I have a template data and of course I have to give it a comment.

90
00:05:11,170 --> 00:05:13,810
Good practice to keep these things up to date as you're doing them.

91
00:05:14,170 --> 00:05:22,360
Template data holds data sent from handlers to templates.

92
00:05:22,660 --> 00:05:23,030
All right.

93
00:05:23,050 --> 00:05:25,030
So I've declared this data type.

94
00:05:25,570 --> 00:05:29,980
So let's go back to our our render package.

95
00:05:30,340 --> 00:05:37,900
And now let's import or add that as a third parameter, which I will just call data or TD for template

96
00:05:37,900 --> 00:05:38,230
data.

97
00:05:38,440 --> 00:05:40,660
And it's going to be of type handlers.

98
00:05:42,750 --> 00:05:44,210
Dort templated.

99
00:05:45,360 --> 00:05:51,740
All right, I'm not using it yet, but down here where I have nil, I probably want to use it.

100
00:05:52,110 --> 00:05:59,700
So in order to do that, I have to make sure, first of all, that there is some kind of data in there,

101
00:05:59,940 --> 00:06:01,020
whatever it may be.

102
00:06:01,170 --> 00:06:02,520
Otherwise, I'm passing nil.

103
00:06:04,530 --> 00:06:08,240
So if you think about it, when I go back to my handlers right away, I have an error.

104
00:06:08,250 --> 00:06:13,620
I have an error because I'm not passing any template data and I can't pass nil because, well, I actually

105
00:06:13,620 --> 00:06:18,660
need template data so I can do that simply by passing an empty template data.

106
00:06:18,930 --> 00:06:28,650
And I can do that with a really simple bit of code and present template data and nothing inside of it.

107
00:06:31,350 --> 00:06:32,260
And I have an error.

108
00:06:32,280 --> 00:06:33,340
Let's go back over here.

109
00:06:33,570 --> 00:06:36,540
Can I use that type template as type hander's template data?

110
00:06:36,550 --> 00:06:39,530
Of course, I don't actually want to pass template data.

111
00:06:39,540 --> 00:06:42,100
I want to pass a pointer to template data.

112
00:06:42,720 --> 00:06:43,340
That's better.

113
00:06:43,620 --> 00:06:47,160
Now, when I go back here, that error should go away and I can do the same thing here.

114
00:06:47,700 --> 00:06:51,570
Ampersand template data with nothing.

115
00:06:52,320 --> 00:06:52,650
All right.

116
00:06:52,650 --> 00:06:53,820
So that should actually work.

117
00:06:54,270 --> 00:06:56,250
But we actually haven't done anything with the data yet.

118
00:06:56,260 --> 00:06:59,040
So let's try putting some logic in here.

119
00:06:59,040 --> 00:07:02,100
Let's just put on our about page.

120
00:07:02,100 --> 00:07:07,620
Let's put a string so I can say, first of all, I can't pass strings.

121
00:07:07,620 --> 00:07:12,690
Remember, template data at the top here says I can pass a string map, so let's create a map.

122
00:07:13,660 --> 00:07:21,570
I'll call it string map is a new make map, string string.

123
00:07:22,230 --> 00:07:26,190
OK, and then we'll say string map test.

124
00:07:26,250 --> 00:07:32,190
That's how I'll look it up in the template is assign the value of hello again.

125
00:07:33,240 --> 00:07:33,630
All right.

126
00:07:34,530 --> 00:07:36,300
Now I've created my string map.

127
00:07:36,310 --> 00:07:41,700
I haven't actually used it yet and I need to pass it as part of template data and template data has

128
00:07:41,700 --> 00:07:43,500
a member called String Map.

129
00:07:43,800 --> 00:07:47,880
And the value I want to assign to that is the string map I just created.

130
00:07:49,500 --> 00:07:55,500
So I've done that now in the template itself, and this is for the About Page, let's go to the about

131
00:07:55,500 --> 00:07:58,410
page and let's see if we can write that.

132
00:07:58,980 --> 00:08:06,600
Now, here's the very first time we're actually pulling any data from our handler into our template,

133
00:08:06,780 --> 00:08:08,610
and we're going to do it really straightforward.

134
00:08:12,270 --> 00:08:18,960
This came from the template and then because of the template directive, it's in those double curly

135
00:08:19,320 --> 00:08:25,530
braces, I want to look up something in the string mat and I can't just go string map and then square

136
00:08:25,530 --> 00:08:26,010
brackets.

137
00:08:26,010 --> 00:08:31,590
And the name that look we are looking for, we have to use the template syntax, which may seem a little

138
00:08:31,590 --> 00:08:33,720
awkward at first, but it works really well.

139
00:08:34,290 --> 00:08:38,070
Index from DOT string map.

140
00:08:38,910 --> 00:08:40,560
I'm looking for the value.

141
00:08:40,680 --> 00:08:42,330
What did I call it in my handler.

142
00:08:43,760 --> 00:08:52,040
Test, so back to the page test, so this says, I'm going to use the function index, I'm looking something

143
00:08:52,040 --> 00:08:54,620
up and something that has an indices or has indices.

144
00:08:55,580 --> 00:08:58,490
So index and I'm looking for string map.

145
00:08:58,490 --> 00:09:04,280
And I put that dot in front of it because the dot refers to all the data that is passed to the template.

146
00:09:04,760 --> 00:09:09,260
And I'm looking for one particular element, string map, and I'm looking for the index test.

147
00:09:10,280 --> 00:09:11,310
So let's give it a try.

148
00:09:11,840 --> 00:09:18,470
Now, the command is, of course, go run command Web Stargirl.

149
00:09:18,800 --> 00:09:22,040
And I'm going to run this and I'm going to tell you right now it's not going to work.

150
00:09:22,040 --> 00:09:29,200
And I have deliberately introduced an error into this project to show you the dreaded import cycle problem.

151
00:09:29,210 --> 00:09:30,700
Let's run it and we'll see what happens.

152
00:09:32,150 --> 00:09:37,490
Package command line arguments, imports, GitHub, Teszler, go course package handlers and render.

153
00:09:37,490 --> 00:09:40,460
And then we have import cycle not allowed.

154
00:09:40,850 --> 00:09:47,000
And this is what I was talking about earlier, what I told you, why I was putting the config into its

155
00:09:47,000 --> 00:09:47,710
own package.

156
00:09:48,500 --> 00:09:52,190
The problem here is that I have packages that are importing each other and you're not allowed to do

157
00:09:52,190 --> 00:09:52,760
that and go.

158
00:09:52,760 --> 00:09:55,100
You can do it in some languages like C++.

159
00:09:55,100 --> 00:09:58,940
You're allowed to well, you have to put some serious hacks in place to make it work.

160
00:09:58,940 --> 00:09:59,960
But you can do that.

161
00:10:00,290 --> 00:10:01,220
You can't do it.

162
00:10:01,220 --> 00:10:06,530
And go goes a little more strict and says, no, sorry, can't have packages that depend upon each other

163
00:10:06,530 --> 00:10:06,980
like that.

164
00:10:07,430 --> 00:10:08,990
And it refuses to compile.

165
00:10:08,990 --> 00:10:11,960
And the irritating thing about it is no idea.

166
00:10:11,960 --> 00:10:16,640
I have ever tried ever warned you about the import cycle while you're putting all the time into writing

167
00:10:16,640 --> 00:10:17,000
code.

168
00:10:17,150 --> 00:10:19,850
It's not until the moment you try to run it that you discover this.

169
00:10:20,030 --> 00:10:22,100
Fortunately, it's not that hard to fix.

170
00:10:22,550 --> 00:10:25,130
And the offending code here, let me hide this.

171
00:10:25,790 --> 00:10:29,330
The offending code is found in handlers and handlers.

172
00:10:29,330 --> 00:10:29,690
Yes.

173
00:10:29,690 --> 00:10:32,930
Where I have this template data, I'm going to pull that right out of there.

174
00:10:33,530 --> 00:10:34,970
We're not going to stick that in there.

175
00:10:34,970 --> 00:10:37,730
Instead, I'm going to create a new package.

176
00:10:40,320 --> 00:10:47,700
Called models, and it's where I'm going to store all the models that I'm going to use in my my application,

177
00:10:47,700 --> 00:10:51,130
and that includes database models and it includes the template data model.

178
00:10:51,660 --> 00:10:55,310
So here I'm going to create a new file and I'm not going to call it models ago.

179
00:10:55,340 --> 00:10:58,410
Let me close that and get rid of that.

180
00:10:59,290 --> 00:11:01,650
OK, I'm going to create a new model.

181
00:11:03,660 --> 00:11:04,350
And file.

182
00:11:08,780 --> 00:11:09,620
A new file.

183
00:11:10,950 --> 00:11:14,250
Called template data go.

184
00:11:15,830 --> 00:11:16,940
And I'll paste that in there.

185
00:11:17,120 --> 00:11:23,270
OK, so now I have this over where it's divorced from everything else, this will never import another

186
00:11:23,270 --> 00:11:23,710
package.

187
00:11:23,720 --> 00:11:27,940
It will only exist to be imported by packages other than itself.

188
00:11:28,550 --> 00:11:29,780
So I can handle it.

189
00:11:29,780 --> 00:11:33,830
Can't be just template data down here at line 31.

190
00:11:33,860 --> 00:11:34,840
I mean, move that up a bit.

191
00:11:35,840 --> 00:11:44,000
Instead, it should be models, template data and this should be models, template data and then in

192
00:11:44,000 --> 00:11:46,100
the handlers, which should have an error now.

193
00:11:48,640 --> 00:11:55,210
So not handlers in where am I rendered render should have an error, you can't be handlers template

194
00:11:55,240 --> 00:11:55,510
data.

195
00:11:55,540 --> 00:12:01,180
It must be models, template data and I can get rid of this at the ID you put in for me.

196
00:12:01,600 --> 00:12:06,670
And I'm not using it yet, but it should at least compile and it should compile.

197
00:12:06,670 --> 00:12:12,070
And one of the thing I forgot to do is here I need to pass in my template data to the buffer.

198
00:12:12,130 --> 00:12:12,510
All right.

199
00:12:12,820 --> 00:12:13,750
So I had two errors.

200
00:12:13,750 --> 00:12:16,040
I put one in on purpose and one that I didn't even notice.

201
00:12:16,570 --> 00:12:18,130
So let's try compiling this again.

202
00:12:23,260 --> 00:12:30,310
It compiled and go back to a Web browser, and now when I load the about page, there is the data that

203
00:12:30,310 --> 00:12:33,630
I've posted and that's exactly what I wanted to have happen.

204
00:12:33,940 --> 00:12:41,590
So I've managed to successfully pass data from my handler to this individual page so that you would

205
00:12:41,590 --> 00:12:42,450
think is the end of it.

206
00:12:42,460 --> 00:12:43,390
But it's not quite.

207
00:12:44,050 --> 00:12:46,650
There are situations, of course, I could do it this way.

208
00:12:46,660 --> 00:12:52,630
I could say every time I'm building a template or rendering a template, I can manually construct all

209
00:12:52,630 --> 00:12:54,930
of the data that I need to pass to that template.

210
00:12:55,390 --> 00:13:01,390
And yet, inevitably, there will be situations where you want certain kinds of data to be available

211
00:13:01,390 --> 00:13:04,960
to every page in the site without adding it by yourself.

212
00:13:05,410 --> 00:13:10,600
So in other words, you want to take a template, data, whatever template that is passed by a handler,

213
00:13:10,930 --> 00:13:16,120
and then add to it data that you want available on every page of your site, which is a pretty straightforward

214
00:13:16,120 --> 00:13:16,600
process.

215
00:13:16,610 --> 00:13:18,660
We may as well do it now just to get it out of the way.

216
00:13:19,210 --> 00:13:26,530
I'm going to do that by coming down to my handlers, my whereas it here, not that one to render.

217
00:13:27,130 --> 00:13:35,650
When I render the page here just before I call that execute function, I'm going to create another function

218
00:13:35,650 --> 00:13:37,170
and I'll put it right above it.

219
00:13:37,180 --> 00:13:46,570
So up here I will call it func add default data and it's going to take one argument and the argument

220
00:13:46,570 --> 00:13:49,870
will be TRD, which is of type.

221
00:13:52,250 --> 00:13:58,850
Points are two models, template data, and it's going to return the same thing, models, template

222
00:13:58,850 --> 00:13:59,150
data.

223
00:14:00,720 --> 00:14:06,480
So that is the function I'm going to call, and right now I don't have any data to add, but I'm going

224
00:14:06,480 --> 00:14:07,740
to just I'll soldiers return.

225
00:14:08,700 --> 00:14:12,920
So all it's doing right now is taking the template data and returning it.

226
00:14:13,290 --> 00:14:19,830
But when I discover to determine exactly what data I want to be available on every page, I can add

227
00:14:19,830 --> 00:14:20,210
it here.

228
00:14:21,150 --> 00:14:27,690
So we come down here and just before I execute that, I simply take I simply say TRD is assigned or

229
00:14:27,690 --> 00:14:32,840
TRD equals add default data and that should work.

230
00:14:33,090 --> 00:14:34,770
So let's try compiling it again.

231
00:14:35,100 --> 00:14:36,270
Stop the application.

232
00:14:36,660 --> 00:14:37,110
Run it.

233
00:14:40,010 --> 00:14:40,550
It's running.

234
00:14:40,670 --> 00:14:42,650
Go to my Web browser, reload the page.

235
00:14:43,040 --> 00:14:45,700
Life is good, no warnings or errors anywhere.

236
00:14:46,130 --> 00:14:49,790
So we're not going to use that ad template, a default data function for a little while.

237
00:14:49,880 --> 00:14:54,980
But it's all set up and easy enough for us to add information to it as needed.

238
00:14:55,340 --> 00:14:55,700
All right.

239
00:14:55,700 --> 00:14:56,530
That's enough for now.

240
00:14:56,810 --> 00:14:58,910
We shall move on to the next section.
