1 00:00:00,250 --> 00:00:02,970 We're getting the page title from a URL parameter and 2 00:00:02,970 --> 00:00:06,230 we're using it to load in page content from a text file. 3 00:00:06,230 --> 00:00:09,700 Now we just need to convert our response to actual HTML and 4 00:00:09,700 --> 00:00:11,350 we can do that using ERB. 5 00:00:12,700 --> 00:00:15,340 Let's talk about the embedding part of embedded Ruby. 6 00:00:16,380 --> 00:00:20,360 You store some static text, usually HTML code, in a file. 7 00:00:20,360 --> 00:00:24,160 Then using specially ERB tags you embed Ruby code into the static 8 00:00:24,160 --> 00:00:27,540 text that displays changing dynamic data. 9 00:00:27,540 --> 00:00:31,120 Each time the template is rendered each piece of Ruby code is evaluated to 10 00:00:31,120 --> 00:00:32,660 get a result. 11 00:00:32,660 --> 00:00:36,500 Those results are inserted into the template in place of the ERB tags, 12 00:00:36,500 --> 00:00:39,460 giving you text that changes each time the template is rendered. 13 00:00:40,540 --> 00:00:44,410 Let's set up a temporary playground where we can try out ERB text. 14 00:00:44,410 --> 00:00:49,424 I'm going to create a new get route with a path of /test. 15 00:00:51,410 --> 00:00:54,260 It's important to place this before the get title route in 16 00:00:54,260 --> 00:00:55,980 your code to avoid a conflict. 17 00:00:55,980 --> 00:00:58,550 We'll talk about the reason for that later. 18 00:00:58,550 --> 00:01:02,500 Within the route block, we'll call the ERB method and 19 00:01:02,500 --> 00:01:04,370 have it render a template called test. 20 00:01:05,710 --> 00:01:11,357 Then we'll create a new file in the views directory called test.erb. 21 00:01:13,943 --> 00:01:17,870 The next time we visit the /test path, it will render the contents of this file. 22 00:01:18,890 --> 00:01:24,340 There are two kinds of ERB, tags, regular embedding tags and output embedding tags. 23 00:01:24,340 --> 00:01:28,640 Regular embedding tags start with <% and 24 00:01:28,640 --> 00:01:32,720 end with %>, they contain some Ruby code in between those symbols. 25 00:01:32,720 --> 00:01:37,190 The code gets evaluated but the results aren't placed directly into the output. 26 00:01:37,190 --> 00:01:42,757 So let's say I had a regular tag like this, grade = 54. 27 00:01:42,757 --> 00:01:45,768 When the tag gets evaluated the grade variable will be set but 28 00:01:45,768 --> 00:01:48,220 nothing will appear in the output. 29 00:01:48,220 --> 00:01:51,813 Regular embedding tags are most frequently used for two things, loops and 30 00:01:51,813 --> 00:01:55,790 including or excluding parts of the template based on the condition. 31 00:01:55,790 --> 00:01:59,835 If you include a conditional like an IF expression in a regular embedding tag, 32 00:01:59,835 --> 00:02:03,380 any text inside the conditional will only be included in the output if 33 00:02:03,380 --> 00:02:04,390 the condition is true. 34 00:02:05,470 --> 00:02:09,138 So let's say that I had a statement up here, 35 00:02:09,138 --> 00:02:13,611 a regular ERB tag setting the grade variable to 97. 36 00:02:13,611 --> 00:02:18,310 And then I'll put insert another ERB tagged with a conditional in it. 37 00:02:18,310 --> 00:02:23,604 if grade, if the grade variable, if the value in it is greater than 60, 38 00:02:23,604 --> 00:02:32,090 I'll put separate ERB tag down here with the end keyword to end the if statement. 39 00:02:32,090 --> 00:02:35,650 Now anything contained inside that conditional is only going to be printed 40 00:02:35,650 --> 00:02:37,370 if the conditional is true. 41 00:02:37,370 --> 00:02:40,810 So let's put a paragraph tag in here saying you passed. 42 00:02:42,700 --> 00:02:47,060 If grade were lower than sixty, you passed would not be included in the output. 43 00:02:47,060 --> 00:02:49,670 But since it's set to ninety seven, it will be. 44 00:02:49,670 --> 00:02:50,530 Let's save this. 45 00:02:51,590 --> 00:02:53,221 And now let's restart our server. 46 00:02:57,904 --> 00:03:00,467 And try visiting the /test path. 47 00:03:02,683 --> 00:03:04,573 It loads the test.arb tag, 48 00:03:04,573 --> 00:03:08,600 evaluates the Ruby code setting the grade variable to 97. 49 00:03:08,600 --> 00:03:12,610 Then it checks the conditional testing whether the grade variable is greater 50 00:03:12,610 --> 00:03:13,400 than 60. 51 00:03:13,400 --> 00:03:17,620 And it is, so it prints the paragraph tag, you passed. 52 00:03:17,620 --> 00:03:21,595 If we change the variable's value to say 54, 53 00:03:21,595 --> 00:03:27,560 save that, and reload the page, the condition 54 00:03:27,560 --> 00:03:32,580 will no longer be true and the you passed HTML will be excluded from the output. 55 00:03:33,600 --> 00:03:36,645 By the way you may have noticed that I didn't restart the app after making 56 00:03:36,645 --> 00:03:38,705 changes to the ERB template. 57 00:03:38,705 --> 00:03:39,485 That's because, 58 00:03:39,485 --> 00:03:44,315 unlike the main Ruby code, ERB templates are reloaded each time they're rendered. 59 00:03:44,315 --> 00:03:46,665 So if you save changes to the ERB template, 60 00:03:46,665 --> 00:03:49,790 they'll show up as soon as you refresh your browser. 61 00:03:49,790 --> 00:03:52,760 The other common use of regular embedding tags is in loops. 62 00:03:52,760 --> 00:03:54,835 If you place a loop in a regular embedding tag, 63 00:03:54,835 --> 00:03:58,580 any text inside the loop lobby will be output repeatedly. 64 00:03:58,580 --> 00:04:02,530 So let's say that I wanted to output three paragraphs containing the word fish. 65 00:04:03,770 --> 00:04:06,970 We'll create a regular embedding tag up here and 66 00:04:06,970 --> 00:04:12,800 we'll loop three times by calling the times method on the integer object three. 67 00:04:12,800 --> 00:04:16,960 Times method takes a block which loops that number of times. 68 00:04:18,010 --> 00:04:22,440 And since it's a do end block we need the end keyword down here in a separate tag. 69 00:04:23,460 --> 00:04:27,590 Now any text we put inside the loop here will be repeated that number of times. 70 00:04:27,590 --> 00:04:33,190 So let's put an HTML paragraph tag, and we'll just put the word fish inside it. 71 00:04:33,190 --> 00:04:35,220 Save this and reload our page. 72 00:04:36,460 --> 00:04:38,070 And you see it loops three times, 73 00:04:38,070 --> 00:04:41,020 each time outputting an HTML paragraph with the word fish. 74 00:04:42,020 --> 00:04:47,420 In addition to regular embedding tags, ERB also has output 75 00:04:47,420 --> 00:04:53,260 embedding tags which begin with <%= and end with %>. 76 00:04:53,260 --> 00:04:56,840 The equal sign at the start is the only difference in how you type output tags 77 00:04:56,840 --> 00:04:58,140 versus regular tags. 78 00:04:59,330 --> 00:05:03,199 Like regular embedding tags the Ruby code inside an output tag gets evaluated when 79 00:05:03,199 --> 00:05:04,740 the template is rendered. 80 00:05:04,740 --> 00:05:08,670 But unlike regular tags, the return value of the Ruby code gets included in 81 00:05:08,670 --> 00:05:11,060 the template in place of the tag. 82 00:05:11,060 --> 00:05:14,973 So if we have an output embedding tag with the math operation 2 + 2, 83 00:05:14,973 --> 00:05:19,980 when the template is rendered the code will be evaluated and the result, four, 84 00:05:19,980 --> 00:05:22,550 will be included in the output. 85 00:05:22,550 --> 00:05:26,910 Similarly, we can place a call to the time.now method in an output tag and 86 00:05:26,910 --> 00:05:28,400 we'll get the current time in the output. 87 00:05:30,340 --> 00:05:33,623 And because the values are updated every time we render the template, 88 00:05:33,623 --> 00:05:36,191 we can refresh the page and see a new result every time. 89 00:05:37,556 --> 00:05:41,800 We can mix regular embedding tags and output embedding tags too. 90 00:05:41,800 --> 00:05:45,500 For example, we can nest an output embedding tag inside an each block and 91 00:05:45,500 --> 00:05:48,640 the output tag will be evaluated repeatedly. 92 00:05:48,640 --> 00:05:52,480 So let's say we wanted to print an HTML paragraph element for 93 00:05:52,480 --> 00:05:54,390 each item in an array. 94 00:05:54,390 --> 00:05:57,331 We can set up the array within a regular embedding tag. 95 00:06:05,256 --> 00:06:09,292 And the each method, if you recall, loops through each element of the array, 96 00:06:09,292 --> 00:06:11,630 passing it to the argument here in the block. 97 00:06:13,270 --> 00:06:15,800 Again this is a multi-line block starting with do, so 98 00:06:15,800 --> 00:06:18,500 we're going to need the end keyword in a separate tag down here. 99 00:06:20,720 --> 00:06:23,550 And any text here within the loop will get repeated. 100 00:06:23,550 --> 00:06:28,480 So we're going to output an HTML paragraph tag and we're going to embed within it 101 00:06:28,480 --> 00:06:33,090 the output number, the current value of the number variable. 102 00:06:35,350 --> 00:06:36,990 Let's save this and reload our page. 103 00:06:38,520 --> 00:06:41,940 And we get the output 1 fish, 5 fish, 25 fish. 104 00:06:41,940 --> 00:06:47,170 It loops through each value and our array, passes it into the number argument to 105 00:06:47,170 --> 00:06:53,420 the block, and then within the loop body this output embedding tag gets evaluated. 106 00:06:53,420 --> 00:06:57,140 The current value of number is the result, and that gets embedded into the output. 107 00:06:58,490 --> 00:07:02,860 So that's how to embed Ruby code into an HTML template using ERB. 108 00:07:02,860 --> 00:07:04,920 We're done trying out ERB code for now, so 109 00:07:04,920 --> 00:07:08,280 let's go into the views directory and delete the test.erb file. 110 00:07:10,365 --> 00:07:14,670 We'll also want to go into the main app code and delete the get /text route