PHP Programming/Why Templating
Tools
General
Sister projects
In other projects
| PHP Programming Why Templating | Templates |
So what is a template, in the first place? For our purpose, a template is an HTML-like document that defines thepresentation of a web page, while a PHP script supplies thecontent. The separation of content and presentation is at the heart of anyGUI paradigm. To see why this is desirable, consider the following hypothetical, yet realistic script. It's a script to retrieve a list of books from the database and display it as a table with alternate colours for each row, or "No books found" if the list is empty.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>List of Books</title> </head> <body><?php$books=newArray();$i=0;$database=open_database();if(is_null($database)){?> <h1>Error opening database</h1> Please contact the system administrator at alice@wonderland.com and report the problem. </body> </html><?phpexit();}$result=$database->query("SELECT ISBN, title, author FROM book");while($row=$result->retrieveAssoc()){$books[]=$row;}?> <table><?phpif(count($books)==0){echo"<tr><td>No books found</td></tr>";}else{foreach($booksas$book){if($i%2==0){echo"<tr bgcolor='#EEEEEE'>";}else{echo"<tr bgcolor='#FFFFFF'>";}$i++;?> <td><?phpecho$book['ISBN'];?></td> <td><?phpecho$book['title'];?></td> <td><?phpecho$book['author'];?></td> </tr><?php}}?> </table></body></html>
You will find yourself writing this kind of script very soon. The problems with this script are obvious:
The list goes on.
| PHP Programming Why Templating | Templates |