Separating code from presentation is a huge deal in any project, from small to large, and it is an excellent techqnique to become accustomed to if you haven't already. Smarty templating is a great way to introduce yourself to this.
This: {html_image file="masthead.gif"} is equivalent to this:
<img src="masthead.gif" border="0" height="48" width="256">
You can probably see right away how you don't need to worry if the image size changes a little (keeping in mind of course how broad a range of image sizes your layout template could handle).
Also, logic on pages is quite simple as well, which we will demonstrate in the guestbook demo shortly.
Once you set up a framework in smarty, making sitewide changes becomes much easier for both the designer and the coder.
require '../libs/Smarty.class.php'
the default (from the regular smarty download) wants you to use your /var/lib/php/smarty directory, but if you have a virtual server you do not have access to this. The code example for this article has this relative path set for you.
You also need to add the directory in the demo folder called templates_c and make sure it's writable, as the program will be expecting it. This directory is where smarty loads your template files and compiles them for quicker execution (refer to the the smarty manual for more info about caching).
You should be able to load the page from http://
The code example for this article has the guestbook demo integrated with it, also containing a tweak to use mysqli instead of PEAR DB, so if you download the original demo archive for the guestbook app, you'll see some subtle differences. Don't forget to run the guestbook sql script so you have the database there! (demo/guestbook/sql/)
Let's take a look at the template file for the guestbook page (demo/guestbook/templates/guestbook.tpl):
{* Smarty *}
<table border="0" bgcolor="#eeeeee" width="300">
<tr>
<th colspan="2" bgcolor="#d1d1d1">Guestbook Entries (<a href="{$SCRIPT_NAME}?action=add">add</a>)</th>
</tr>
{foreach from=$data item="entry"}
<tr>
<td>{$entry.Name|escape}</td>
<td align="right">{$entry.EntryDate|date_format:"%e %b, %Y %H:%M:%S"}</td>
</tr>
<tr>
<td colspan="2" bgcolor="#dedede">{$entry.Comment|escape}</td>
</tr>
{foreachelse}
<tr>
<td colspan="2">No records</td>
</tr>
{/foreach}
</table>
Take note of the $data variable; this is passed from the index page in the command:
$guestbook->displayBook($guestbook->getEntries());
function displayBook($data = array()) {
$this->tpl->assign('data', $data);
$this->tpl->display('guestbook.tpl');
}
function getEntries() {
$out = array();
$result = $this->sql->query(
"select * from GUESTBOOK order by EntryDate DESC"
);
while ($row = $result->fetch_assoc()) {
array_push($out, $row);
}
//print_r($out);
return $out;
}
$this->tpl->display('guestbook.tpl');
Taking a close look at how this code is organized will really get you started in how sophisticated you can get with this system. Take special note again of how simple the index page and template files are in this application! As a coder, you would have full control over logical execution of code on the index page and all background class files. The only unknowns would be if your designer mucks with your smarty tags in the template files, but that won't be happening, now, will it?