Implementing a template based website

A closer look

Lets take a look at

     eval("\$resultbits .= \"".gettemplate("ResultsBit")."\";");

first. What the eval statement does is evaluate php code as if you have written it yourself. For example

eval("echo \"thetext\";");

does the same as hard-coding

echo "thetext";

Our code takes advantage of this functionality, by writing it's own php code for this statement. What's the point, you may ask? Well, just as

echo "hello $name";

would output hello, and the contents of the variable, $name, the same applies when used within an eval statement. Lets imagine that gettemplate function returns simply

<td>$data[title]</td>

What ends up being evaluated in the call to the eval statement is

$resultbits .= "<td>$data[title]</td>";

(remembering that we need to escape characters such as $ and " within the string). PHP then appends <td>$data[title]</td> to the $resultbits variable, and replaces $data[title] with its current value. In the same way, PHP replaces all the variables within our templates with the current value (which, in this instance, has been loaded from $data=mysql_fetch_array($query).) Our code simply loops through the information from the database, evaluating the template for each different value of $data[title] and appends it to $resultbits. Finally, once the loop is complete, we call another eval statement, this time retrieving the value from the ResultsPage template, and replacing $pagetitle,$searchquery and $resultbits with their values in the script. Simple! :-)

And there you have it, your own template based website.

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Memory is like an orgasm. It's a lot better if you don't have to fake it.” - Seymour Cray