1
00:00:00,900 --> 00:00:05,850
So we left off here last time and what we did was we created this nice render template test function

2
00:00:06,390 --> 00:00:12,390
to actually go and pass all of our templates, including the layouts, and store them in this variable.

3
00:00:12,390 --> 00:00:19,080
My cache and my cache is a map and I can look up values in that map by a string and the string in this

4
00:00:19,080 --> 00:00:22,680
case, the thing I look up values in the map by happens to be the name of the template.

5
00:00:22,890 --> 00:00:27,560
So something like about page temple or homepage dot temple.

6
00:00:27,900 --> 00:00:33,030
And once I look them up I actually get a pointer to that ready to use template, which is exactly what

7
00:00:33,030 --> 00:00:35,520
I need to render pages to the site.

8
00:00:35,910 --> 00:00:41,520
So this actually creates that cache, that, that map that I can use that I need to be able to use.

9
00:00:41,940 --> 00:00:43,080
Where can I use that?

10
00:00:43,110 --> 00:00:47,660
Well, the logical place to use that, of course, is here in this render template function.

11
00:00:48,060 --> 00:00:52,860
But the first thing I'm going to do is get rid of all of this code that did nothing more than show that

12
00:00:52,860 --> 00:00:55,190
that that we were building the cache properly.

13
00:00:55,200 --> 00:00:56,290
That's of no value to us.

14
00:00:56,290 --> 00:00:58,790
So let's get rid of it so we can get rid of that there.

15
00:00:58,800 --> 00:01:04,620
And then I have to re initialize error because it's the first time I'm using it and there's a line down

16
00:01:04,620 --> 00:01:06,930
here where I can get rid of that as well.

17
00:01:07,170 --> 00:01:07,610
All right.

18
00:01:08,130 --> 00:01:14,640
So we're I mean, this is the logical place to use that map of of templates, our template cache.

19
00:01:14,910 --> 00:01:18,480
So I need to get this value that's produced right here, up here.

20
00:01:18,720 --> 00:01:20,700
So the obvious thing to do is to call this function.

21
00:01:20,880 --> 00:01:24,720
But first, let's give it a more meaningful name rather than calling it render template test, which

22
00:01:24,720 --> 00:01:25,500
is not a great name.

23
00:01:25,740 --> 00:01:28,830
Let's call it create template cache.

24
00:01:29,370 --> 00:01:34,200
And I'm not ever using this response rate or as a parameter, so I'm going to delete that for now.

25
00:01:34,200 --> 00:01:35,460
I might need it later on.

26
00:01:35,460 --> 00:01:36,790
If I do, I'll add it back in.

27
00:01:37,920 --> 00:01:40,770
So up here, I probably should give this a comment.

28
00:01:40,780 --> 00:01:42,600
So let's do that now to get it out of the way.

29
00:01:46,000 --> 00:01:49,990
Creates a template cache as a map.

30
00:01:50,890 --> 00:01:53,260
All right, I need called here.

31
00:01:53,290 --> 00:01:54,210
That's all I need to do.

32
00:01:54,220 --> 00:02:00,400
And this takes no parameters now because we got rid of the response rate and it returns a map and an

33
00:02:00,400 --> 00:02:00,730
error.

34
00:02:00,730 --> 00:02:02,000
So it returns to things.

35
00:02:02,020 --> 00:02:03,670
So let's just call it simple enough.

36
00:02:04,690 --> 00:02:07,810
We'll call our template cache T.C. and we'll check for an error.

37
00:02:07,840 --> 00:02:10,690
And that's a sign the value of create template cache.

38
00:02:11,020 --> 00:02:12,670
And then we have to check for an error.

39
00:02:12,700 --> 00:02:17,770
If error is not equal to nil, then I actually want to die at this point.

40
00:02:17,770 --> 00:02:20,530
And there's better error handling we'll be looking at later on.

41
00:02:20,530 --> 00:02:24,610
But for this, for the purposes of what we're trying to do right now, we're just going to die.

42
00:02:24,640 --> 00:02:27,730
So I'll say log fatele and an error.

43
00:02:27,910 --> 00:02:32,170
And that stops the application at this point because we can't get our template cache, therefore we

44
00:02:32,170 --> 00:02:33,130
can't show any pages.

45
00:02:33,310 --> 00:02:34,740
There's no point in going any further.

46
00:02:35,860 --> 00:02:41,770
So assuming we get past this, which we will because we know this works, how are we going to actually

47
00:02:41,770 --> 00:02:45,240
do something with an individual template when we pull that value out of the map?

48
00:02:45,370 --> 00:02:50,440
So I could just say my template is equal to and and pull it out of the map.

49
00:02:50,450 --> 00:02:57,310
So my map is called T.C. and I could pull the template name, which we know is called Tapio, the variable

50
00:02:57,310 --> 00:03:02,620
here, so we could copy that and paste it in here and that will actually work.

51
00:03:03,100 --> 00:03:06,790
But what if the template I'm trying to pass doesn't exist?

52
00:03:07,000 --> 00:03:13,280
So I'm passing it instead of passing about page up to Templer home page that template.

53
00:03:14,170 --> 00:03:19,300
What if the parameter is something like Yellow Pages template, which doesn't exist anywhere in the

54
00:03:19,300 --> 00:03:19,630
map?

55
00:03:19,750 --> 00:03:25,420
Well, one of the very common ways of checking for this sort of thing is actually to assign to variables

56
00:03:25,420 --> 00:03:25,700
here.

57
00:03:26,320 --> 00:03:33,940
So what will happen is if the template I'm trying to pull out exists, then TI will have a value and

58
00:03:33,940 --> 00:03:35,890
this will be set to true.

59
00:03:36,340 --> 00:03:40,950
But if it doesn't exist, this won't have any useful value and this will be default.

60
00:03:40,960 --> 00:03:47,230
So I can, this will be set defaults so I can check for that by saying if not OK then what I want to

61
00:03:47,230 --> 00:03:47,560
do.

62
00:03:47,560 --> 00:03:52,840
Well if I can't find the template again for the purposes of what we're doing right now, I'm just going

63
00:03:52,840 --> 00:03:53,280
to die.

64
00:03:54,100 --> 00:03:56,710
So if we get past this, then we have our template cache.

65
00:03:56,860 --> 00:04:00,260
If we get past this, then we have the template we want to use.

66
00:04:00,880 --> 00:04:04,810
So how do we use a template that's not read from disk?

67
00:04:05,170 --> 00:04:09,790
What we were doing before down here and all of this stuff we're going to get rid of very shortly is

68
00:04:09,790 --> 00:04:14,230
we actually read the template from the disk, then we pass the template and we're done.

69
00:04:14,950 --> 00:04:16,690
In this case, we can't do that.

70
00:04:16,690 --> 00:04:20,920
And we can't do that for a very simple reason because we haven't read it from disk.

71
00:04:21,220 --> 00:04:23,100
So we need to handle a little bit differently.

72
00:04:23,110 --> 00:04:29,320
And what I'm going to do here is create something called a bytes buffer, and I'm going to give it a

73
00:04:29,320 --> 00:04:34,650
name and that's going to be assigned the value of new bytes dot buffer.

74
00:04:36,430 --> 00:04:39,120
So this is a buffer that will hold the information.

75
00:04:39,660 --> 00:04:41,110
All of this can do is hold bytes.

76
00:04:41,110 --> 00:04:48,310
And I need to put that value, that PERS template that I currently have in memory into some bytes.

77
00:04:48,340 --> 00:04:51,390
So all I'm going to do is very similar to what we did before.

78
00:04:51,730 --> 00:04:57,940
I'm going to not check for an error at this point equals t my template.

79
00:04:59,260 --> 00:05:00,020
Execute.

80
00:05:01,630 --> 00:05:02,740
And I'm going to execute.

81
00:05:04,920 --> 00:05:12,750
To the buffer, the value of that template somewhat taking saying take this template that I have executed,

82
00:05:12,900 --> 00:05:18,060
don't pass it any data and store the value in this buff variable.

83
00:05:18,600 --> 00:05:23,740
Now, I have exactly what I need to write to my response writer so I can just say Buffer.

84
00:05:24,000 --> 00:05:24,440
Right.

85
00:05:25,440 --> 00:05:27,080
Try that again, Buff Dot.

86
00:05:27,150 --> 00:05:27,590
Right.

87
00:05:27,600 --> 00:05:32,580
And I'm going to write to my response writer, and that should write everything to the template, which

88
00:05:32,580 --> 00:05:33,830
means I can get rid of this.

89
00:05:35,100 --> 00:05:38,630
Now, this if I look at right to what is it written, it actually returns an error.

90
00:05:38,640 --> 00:05:42,960
So technically I should say error equals and then check for that error.

91
00:05:43,050 --> 00:05:47,820
If error is not equal to nil, then and I can just print the error.

92
00:05:47,820 --> 00:05:50,010
What do I have here now?

93
00:05:50,040 --> 00:05:51,120
It gets two returns.

94
00:05:51,120 --> 00:05:52,110
And what does it return?

95
00:05:52,110 --> 00:05:56,850
It returns and oh yes, the number of bytes and I don't care about the number of votes, so I'll get

96
00:05:56,850 --> 00:05:57,300
rid of that.

97
00:05:57,300 --> 00:05:57,960
Just ignore it.

98
00:05:58,470 --> 00:06:00,870
And if there's an error at this point, I don't want the application to die.

99
00:06:01,020 --> 00:06:03,330
I'm just going to write to the airlock.

100
00:06:03,330 --> 00:06:13,410
So I'll just say format the print line error writing template to browser followed by the error.

101
00:06:15,030 --> 00:06:15,490
All right.

102
00:06:15,990 --> 00:06:19,680
In theory, this should work because I've clean things up a little bit.

103
00:06:19,800 --> 00:06:22,410
And again, we're going to do improved error handling later on.

104
00:06:22,410 --> 00:06:25,470
But right now, I just want to get templates written to the screen.

105
00:06:25,830 --> 00:06:26,700
So let's try this.

106
00:06:27,060 --> 00:06:33,930
Let's open our terminal, clear the screen and go run command web.

107
00:06:34,440 --> 00:06:35,300
Start up, go.

108
00:06:35,430 --> 00:06:36,360
Let's see if it runs.

109
00:06:37,080 --> 00:06:37,830
That looks good.

110
00:06:38,130 --> 00:06:42,690
Let's go over to our Web browser and I'm on localhost eighty eighty in the last time I was here.

111
00:06:42,690 --> 00:06:43,770
Just give me a blank screen.

112
00:06:43,770 --> 00:06:47,340
So this time I hope to see the homepage and there it is.

113
00:06:47,790 --> 00:06:50,940
Now what about going to the about page.

114
00:06:51,570 --> 00:06:52,860
That's good as well.

115
00:06:53,190 --> 00:06:53,970
That's perfect.

116
00:06:54,360 --> 00:07:00,630
OK, it's almost perfect because if you think about it, we've actually although we're now using layouts

117
00:07:00,630 --> 00:07:02,670
and you can see that the layout is working properly.

118
00:07:02,670 --> 00:07:08,220
And if I view the source from the actual page, this part comes out of the layout document.

119
00:07:08,490 --> 00:07:10,590
This part comes out of the layout document.

120
00:07:10,770 --> 00:07:14,900
And this is the only thing that's being rendered from the about template.

121
00:07:15,060 --> 00:07:19,850
So this text here is exactly what I see here.

122
00:07:21,570 --> 00:07:25,530
So everything seems to be working, but let's look at our code again.

123
00:07:26,430 --> 00:07:27,240
Render Dutko.

124
00:07:27,240 --> 00:07:27,770
Here we are.

125
00:07:28,350 --> 00:07:34,500
What are we really doing every single time we go to the to render any page on the site?

126
00:07:34,770 --> 00:07:40,200
This function is getting called render template and it receives a response writer and it receives the

127
00:07:40,200 --> 00:07:41,760
name of the template I want to render.

128
00:07:42,030 --> 00:07:50,010
Then every single time we open a page on the site, we're building this template cache that seems really

129
00:07:50,010 --> 00:07:50,760
inefficient.

130
00:07:51,450 --> 00:07:56,520
Before we made these changes, we were just loading a single template, parsing it and writing it to

131
00:07:56,520 --> 00:07:58,320
the to the screen, to the browser window.

132
00:07:58,740 --> 00:08:05,670
But now every single time we render a single page on the site, we're actually parsing all of our template

133
00:08:05,670 --> 00:08:06,150
files.

134
00:08:06,480 --> 00:08:11,640
Now, go is very fast and you're not going to notice this if you don't have any appreciable amount of

135
00:08:11,640 --> 00:08:12,600
traffic on your site.

136
00:08:12,990 --> 00:08:16,950
But it is grossly inefficient and there's got to be a better way to do this.

137
00:08:17,220 --> 00:08:20,790
So in the next lecture, we'll figure out a better way to do that.
