>> DAVID MALAN: Let's improve the course's homepage a bit further by
generalizing those functions, renderHeader and renderFooter, into,
quite simply, render because after all, they were nearly identical except
for their names.
How to do this?
Well atop this file just, below where we require helpers.php, let's call a
new function, render, whose first argument, let's say, shall be the name
of a template, a file that's meant to be plugged in there.
Meanwhile, the second argument shall remain an associative array, in this
case an array containing a key for title whose value is here CS50.
Let's now look at helpers.php.
>> In helpers.php, we now, of course, have just one
function defined, render.
render takes now two arguments, which we'll call template and data, the
latter of whose default value is still an empty array.
But in this version of render, notice that I now declare a local variable
called path and assign it the value of template, which, for instance, might
be quote, unquote, "header" and append to that string .php.
In other words, let's assume for now that if our template is called header,
then that template will be implemented in a file called, quite simply,
header.php.
>> Meanwhile, let's do a quick sanity check.
If that path exists, which we can check by way of a file called
file_exists, then let's extract the associative array called data so that
we have one or more local variables in scope, and then let's
require that path.
Meanwhile, helpers.php looks quite simply like this, exactly as before.
>> But if we now look back at index.php, notice that at the bottom of that
file, we can similarly call render, passing in as its
sole argument footer.
In this case, we don't need to pass in an associative array because the
footer template, recall, is just this.
But now we have arguably an even cleaner design because we've
eliminated some of our otherwise redundant code and consolidated into
just one function, render.