Have you ever been annoyed by template engines that force you to keep your templates in flat files? Ever feel like that Content Management System you just created isn't as dynamic as you originally thought? If you have ever felt like this, you may benefit from having your templates in a database.
First, this assembles the query, and selects one row's content from the templates table, but only when it's the one asked for by $templateName. Then we perform the query, and store the resource into the variable $tRes. Next, we use the mysql_fetch_array(); function to store the result of the query into an array. The next part maybe confusing, so firstly, I'll explain what we do with the result of the function.
To show the template page_header, we would use this code:
<?php
eval (fetchTemplate("page_header"));
?>
This uses a function you may not have come across before, eval();. You can find more information about this function at www.php.net/eval. Basically, it parses the string you pass it as PHP.
So if we go back up to the function fetchTemplate, and to the line $Content = "echo "$Template[content]";"; we can see that this is preparing the string for use with eval(); What it is doing is putting echo " in front of the template's content, and "; after it. If we use the short template page_footer as an example, the value of $Content would be the following:
echo "</body>rn</html>";
Therefore, when we use this string in eval, it would display the template's content onto the user's browser.
Other Functions
Now that we've got the hard bit out of the way, the pageHeader & pageFooter functions simply provide a nice easy function to put at the top & bottom of every page, instead of having to use eval (fetchTemplate("page_header"));
However, if you remember the content of the page_header template, it contained a variable in it.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>$pageTitle</title> </head> <body>
And if we look at the pageHeader function, the $pageTitle variable is passed into it, so when the code is eval'ed in the function, $pageTitle is replaced with whatever was passed to it in the function.