Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 56,721 times

Contents

Downloads

Related Categories

Implementing a template based website - A closer look

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.

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • using include with a php marked-up htm template

    Posted by aaron123nz on 02 Jun 2005

    I have been trying to use php's inlude to include a template file which has been marked up with some php variables. However when using include() the php parsing drops out. This code keeps the parsin...

  • Code Errors

    Posted by jaam on 25 Feb 2004

    This sample is great BUT have a cuople of errors.
    The file test.php is missing "#include template.php";.
    The file template.php presents a variable $name that must be repalced with $templatename.
    If...

  • Posted by James Crowley on 22 Feb 2004

    The article was written before PHP had made this a requirement - I'll try and get the article updated to reflect these changes. :)

  • Posted by James Crowley on 22 Feb 2004

    The article demonstrates two methods - one that uses a MySQL database to store the templates, and one that just uses a folder on the website.

  • Don't understand

    Posted by johnlcox on 26 Nov 2003

    I'm having a really hard time understanding how this works and I am not able to test it since I don't have a mysql server setup on my computer. Does anyone have an example they could show me that doe...