1 00:00:00,000 --> 00:00:02,420 >> DAVID MALAN: Let's improve the course's homepage a bit further by 2 00:00:02,420 --> 00:00:06,180 generalizing those functions, renderHeader and renderFooter, into, 3 00:00:06,180 --> 00:00:10,510 quite simply, render because after all, they were nearly identical except 4 00:00:10,510 --> 00:00:11,460 for their names. 5 00:00:11,460 --> 00:00:12,290 How to do this? 6 00:00:12,290 --> 00:00:17,140 Well atop this file just, below where we require helpers.php, let's call a 7 00:00:17,140 --> 00:00:20,610 new function, render, whose first argument, let's say, shall be the name 8 00:00:20,610 --> 00:00:23,850 of a template, a file that's meant to be plugged in there. 9 00:00:23,850 --> 00:00:27,690 Meanwhile, the second argument shall remain an associative array, in this 10 00:00:27,690 --> 00:00:32,659 case an array containing a key for title whose value is here CS50. 11 00:00:32,659 --> 00:00:35,240 Let's now look at helpers.php. 12 00:00:35,240 --> 00:00:37,900 >> In helpers.php, we now, of course, have just one 13 00:00:37,900 --> 00:00:39,570 function defined, render. 14 00:00:39,570 --> 00:00:43,090 render takes now two arguments, which we'll call template and data, the 15 00:00:43,090 --> 00:00:46,310 latter of whose default value is still an empty array. 16 00:00:46,310 --> 00:00:50,180 But in this version of render, notice that I now declare a local variable 17 00:00:50,180 --> 00:00:54,540 called path and assign it the value of template, which, for instance, might 18 00:00:54,540 --> 00:00:59,680 be quote, unquote, "header" and append to that string .php. 19 00:00:59,680 --> 00:01:03,830 In other words, let's assume for now that if our template is called header, 20 00:01:03,830 --> 00:01:07,120 then that template will be implemented in a file called, quite simply, 21 00:01:07,120 --> 00:01:09,170 header.php. 22 00:01:09,170 --> 00:01:11,140 >> Meanwhile, let's do a quick sanity check. 23 00:01:11,140 --> 00:01:14,920 If that path exists, which we can check by way of a file called 24 00:01:14,920 --> 00:01:19,850 file_exists, then let's extract the associative array called data so that 25 00:01:19,850 --> 00:01:22,940 we have one or more local variables in scope, and then let's 26 00:01:22,940 --> 00:01:24,850 require that path. 27 00:01:24,850 --> 00:01:30,040 Meanwhile, helpers.php looks quite simply like this, exactly as before. 28 00:01:30,040 --> 00:01:34,510 >> But if we now look back at index.php, notice that at the bottom of that 29 00:01:34,510 --> 00:01:37,570 file, we can similarly call render, passing in as its 30 00:01:37,570 --> 00:01:39,060 sole argument footer. 31 00:01:39,060 --> 00:01:42,190 In this case, we don't need to pass in an associative array because the 32 00:01:42,190 --> 00:01:44,540 footer template, recall, is just this. 33 00:01:44,540 --> 00:01:47,650 But now we have arguably an even cleaner design because we've 34 00:01:47,650 --> 00:01:51,360 eliminated some of our otherwise redundant code and consolidated into 35 00:01:51,360 --> 00:01:52,820 just one function, render. 36 00:01:52,820 --> 00:01:54,304