Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,350 --> 00:00:08,940
Now in the last lesson we looked at how we were able to pass variables from our server over to our template
2
00:00:08,940 --> 00:00:13,400
file using EJS and these little markers.
3
00:00:13,440 --> 00:00:20,630
Now in this lesson I want to talk about how you can use EJS to run code inside your template.
4
00:00:20,940 --> 00:00:28,200
So for example if we wanted to check to see what was the value of the kindOfDay that was passed in
5
00:00:28,800 --> 00:00:35,340
and if the value was something like Saturday or Sunday then we're going to render our h1 slightly
6
00:00:35,430 --> 00:00:38,770
differently than if it happened to be a weekday.
7
00:00:39,000 --> 00:00:48,860
Now instead of simply saying it's a Thursday or it's a Friday, we're actually name our h1 as our list title.
8
00:00:48,900 --> 00:00:55,070
So h1 is going to instead be something like Thursday list or Friday list.
9
00:00:55,170 --> 00:01:03,280
And what I want to be able to do is I want to be able to check to see if the day is a Saturday or a Sunday.
10
00:01:03,510 --> 00:01:09,560
And in that case I'm going to change the color of my h1 depending on that information.
11
00:01:09,600 --> 00:01:16,110
So if I were to write it out in plain old Javascript, then I would say something like If the value
12
00:01:16,140 --> 00:01:28,320
of the variable kindOfDay is equal to the string that is a Saturday or if kindOfDay was equal
13
00:01:28,500 --> 00:01:34,560
to Sunday, then in that case I'm going to render this
14
00:01:34,710 --> 00:01:44,190
h1 as Saturday list or maybe say something like Saturday to do list, Sunday to do list, but I'm going
15
00:01:44,190 --> 00:01:53,330
to change the h1's style attribute to change the color when it is a Saturday or Sunday to purple.
16
00:01:53,430 --> 00:02:00,300
But if the kindOfDay was not a Saturday or Sunday namely it was a weekday then I'm going to change
17
00:02:00,300 --> 00:02:07,120
the h1 to have a different color, let's say I don't know, maybe change it to blue.
18
00:02:07,350 --> 00:02:10,860
So this is how I would write it out in Javascript.
19
00:02:10,860 --> 00:02:17,820
Now if you take a look at the EJS docs section, you can see that there is a part where it talks about
20
00:02:18,180 --> 00:02:20,040
the different tags.
21
00:02:20,130 --> 00:02:27,660
And previously we've been using this particular tag which as it says outputs the value of whatever goes
22
00:02:27,660 --> 00:02:37,020
inside the tag into the template. And that is how we manage to get the day of the week into our h1.
23
00:02:37,020 --> 00:02:43,920
Now in this case, what we're doing is we're using some control flow so namely an if and else statement,
24
00:02:44,310 --> 00:02:49,860
to check what is the value of that kindOfDay variable that we got passed in here
25
00:02:50,190 --> 00:02:58,170
and if it is a weekend a Saturday or Sunday, then we're rendering a different h1 style compared to if
26
00:02:58,170 --> 00:02:59,860
it was the week day.
27
00:03:00,120 --> 00:03:03,340
So in this case we need to use a different tag.
28
00:03:03,630 --> 00:03:08,000
And the one that we're going to be using is this one which has a scriptlet tag.
29
00:03:08,040 --> 00:03:13,550
So it's not a full script tag because you can't write any sort of Javascript in here
30
00:03:13,740 --> 00:03:17,570
but it does allow you to use control flow,
31
00:03:17,850 --> 00:03:26,950
namely IF and ELSE statements or ELSE-IF or Switch statements or WHILE statements. And this allows you to render
32
00:03:26,950 --> 00:03:33,990
a different HTML depending on what was the value of the variable that you got passed in to the
33
00:03:33,990 --> 00:03:34,780
template.
34
00:03:35,130 --> 00:03:42,150
So the special thing about the scriptlet tag is that you have to added around any part of your code that
35
00:03:42,150 --> 00:03:43,970
is not HTML.
36
00:03:44,040 --> 00:03:46,910
So this is HTML,
37
00:03:46,930 --> 00:03:55,950
it's an h1 tag. But this is not HTML email so we can add our scriptlet tags in front of our Javascript
38
00:03:55,980 --> 00:03:56,810
basically.
39
00:03:57,060 --> 00:04:04,110
So I'm going to add a open angle bracket, a percentage sign and then I'm going to end the line with a
40
00:04:04,110 --> 00:04:09,900
percentage and angle bracket. And then we're going to add one over here as well,
41
00:04:09,900 --> 00:04:12,530
the line of the statement.
42
00:04:12,600 --> 00:04:21,260
And finally remember every single little bit of your code that is Javascript, you will need to add the scriptlet
43
00:04:21,260 --> 00:04:27,550
tags. And these scriptlet tags work on a line by line basis.
44
00:04:27,570 --> 00:04:33,730
So even if you had two lines of Javascript that were next to each other, you still have to add a
45
00:04:33,750 --> 00:04:39,060
open and close tag on each line of the JavaScript.
46
00:04:39,060 --> 00:04:43,500
You can't just add a start one and add a end one, it doesn't work.
47
00:04:43,500 --> 00:04:46,820
So now let's hit save and let's try it out.
48
00:04:46,980 --> 00:04:55,080
So it says it's the Thursday to do list and it's colored blue because we're landing in the else statement
49
00:04:55,080 --> 00:04:55,640
here.
50
00:04:55,830 --> 00:05:04,200
So let's try changing this to Thursday. And now we're checking if the kindOfDay is equal to Thursday
51
00:05:04,200 --> 00:05:12,210
in which case render it with the color purple. And you can see when I update, this h1 is now colored
52
00:05:12,210 --> 00:05:13,060
purple.
53
00:05:13,290 --> 00:05:21,080
So the scriptlet tag allows us to run some basic Javascript code namely for control flow purposes
54
00:05:21,500 --> 00:05:28,920
so we can render our HTML slightly differently depending on the value of the variable that we got passed
55
00:05:28,920 --> 00:05:29,490
in.
56
00:05:29,490 --> 00:05:37,230
Now the reason why the scriptlet tag only lets you use control flow Javascript is because in most cases
57
00:05:37,320 --> 00:05:41,170
you want to keep your logic inside your server.
58
00:05:41,640 --> 00:05:46,890
And it's only in the cases where you're really changing something that is difficult to do inside the
59
00:05:46,890 --> 00:05:51,260
server or it would be a lot quicker to do inside the template
60
00:05:51,420 --> 00:05:53,900
do you actually use the scriptlet tags.
61
00:05:53,970 --> 00:06:00,390
And you see a lot of people in the wild abusing these trying to stuff too much logic into what is essentially
62
00:06:00,390 --> 00:06:01,970
still the content file.
63
00:06:01,980 --> 00:06:04,070
Remember HTML is for content.
64
00:06:04,350 --> 00:06:11,190
So remember try to do all of your logic inside your server and only in select situations where it really
65
00:06:11,190 --> 00:06:15,120
is about modifying the content based on the variable
66
00:06:15,120 --> 00:06:21,660
do you actually add in the scriptlet tags. Now in the next lesson, we're going to build out the functionality
67
00:06:21,660 --> 00:06:23,120
of our to do list.
68
00:06:23,160 --> 00:06:29,230
We're going to talk about how we can pass data from our template back to our server.
69
00:06:29,340 --> 00:06:33,360
So for all of that and more, I'll of course see you on the next lesson.
7652
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.