>> SPEAKER: Let's improve the courses homepage by allowing ourselves to
provide custom titles for each of those pages, so that each of them is
not, quite simply, CS50.
To do this, let's implement a couple of functions, one called Render Header
and one called Render Footer, that instead of Require Alone will take
care of the rendering of our page's headers and footers.
Let's take a look.
>> Inside of index.php now, notice that I'm requiring a new file called
Helpers.php.
It's in there that my two functions will be defined.
Below that, notice that I'm now calling that function Render Header.
And I'm passing in an argument.
>> The type of that argument appears to be an array, which in PHP is denoted
with square brackets.
In particular, this array is an associative array insofar as it
associates a key, like title, with a value, like CS50, rather than relying
on numeric indices alone.
Below that now we have my unordered list.
But below that, we have a call to Render Footer, the other function
that's presumably defined in Helpers.php.
>> Let's take a look there now.
In Helpers.php, notice that I have, indeed, that function Render Footer
whose purpose in life is apparently to take in as its argument an array
called Data.
Now, if the user does not provide an explicit array, the fact that there's
an equal sign followed by empty square brackets means that the default value
of Data will simply be an empty array.
>> Inside of Render Footer, notice that I'm calling a function called Extract.
Extract takes an associative array, like Data in this case, and for any
key in it, it turns that key into a local variable of the same name.
So if that associative array had a key called Foo, extract would ensure that
we now have a local variable called dollar sign Foo, whose value is the
same as it was in that associative array.
>> Lastly, I call Require Footer.php, which as an aside, looks
quite simply like this.
Now, back in Helpers.php, notice that we also have Render Header, whose
argument is the same, an array called Data whose default
value is an empty array.
We then call Extract again, as before.
And then we Require Header.php, whose contents are now this.
>> Inside of Header.php, notice that we no longer have a hard coded title.
And we now have an H1 tag, as well.
In particular, the value of the title is going to be whatever the value of
the local variable called Title is.
After passing it to a PHP function called HTML Special Chars, we're going
to obtain that return value, and then plug it into the HTML I'm outputting
by a way of this function, called Echo.
>> Really, this is shorthand notation for a function call that might otherwise
look like this.
But it's a lot cleaner, simply, to write open bracket question mark equal
sign, without even mentioning PHP.
I'm now going to do the same inside of that H1 tag so that ultimately, if the
user passes to my Render Header function an associative array, inside
of which is a key called Title, that associative array's key will become a
local variable called dollar sign Title.
>> We will then pass that dollar sign Title variable to this function, HTML
Special Chars.
And just in case there's any potentially dangerous characters in
there, like ampersands or other such symbols, HTML Special Chars will make
sure that they're rendered safely on the page.
And by using open bracket question mark equal sign, I'm literally
substituting in at that part of the page whatever the return value
of HTML Chars is.
>> Meanwhile, in Lectures.php, I have a new value for Title.
Not CS50, but Lectures.
In Week0.php, I have a value of Week 0.
And in Week1.php, I have a value of Week 1.
So at the end of the day, my page looks quite like it did before.
But we've now gotten a much, much better design.