Sample Application: Guestbook
We have two templates for our guestbook, one for viewing and one for adding a
new entry.
| /web/www.example.com/smarty/guestbook/templates/guestbook.tpl |
{* Smarty *}
<table border="0" 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 bgcolor="{cycle values="#dedede,#eeeeee" advance=false}">
<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="{cycle values="#dedede,#eeeeee"}">{$entry.Comment|escape}</td>
</tr>
{foreachelse}
<tr>
<td colspan="2">No records</td>
</tr>
{/foreach}
</table>
|
guestbook.tpl is the template for viewing the guestbook. It loops over
the guestbook data (which was assigned from displayBook()) in a
foreach loop and displays the Name, Date and Comment from each entry. The
date is formatted with the date_format modifier. The Name and Comment are
HTML-escaped using the escape modifier to avoid any HTML tag clashes or
scripting attacks. The {cycle} function is used to cycle through background
colors every two table rows.
| /web/www.example.com/smarty/guestbook/templates/guestbook_form.tpl |
{* Smarty *}
<form action="{$SCRIPT_NAME}?action=submit" method="post">
<table border="1">
{if $error ne ""}
<tr>
<td bgcolor="yellow" colspan="2">
{if $error eq "name_empty"}You must supply a name.
{elseif $error eq "comment_empty"} You must supply a comment.
{/if}
</td>
</tr>
{/if}
<tr>
<td>Name:</td>
<td><input type="text" name="Name" value="{$post.Name|escape}" size="40"></td>
</tr>
<tr>
<td valign="top">Comment:</td>
<td><textarea name="Comment" cols="40" rows="10">{$post.Comment|escape}</textarea></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
|
guestbook_form.tpl is the template for adding an entry to the guestbook.
If the form is being redisplayed due to a validation error, the form values are
repopulated and the appropriate error message is displayed. The form values are
HTML-escaped so there are no HTML tag or quote character clashes (very
important!)
With this sample application we have accomplished several key aspects of a Smarty driven application.
* All presentation elements are contained in the template. We don't assign HTML
tags or other presentation elements from outside the template. The only thing we
assign is the page content, in this case the guestbook entries.
* Error messages are also maintained from the template. We don't assign error
messages themselves, but error codes which are used to determine which error
message to display. An alternative way to maintain error messages are from within
Smarty config files, where you can have error_code = Error Message in the
config file, then displayed with {$smarty.config.$error_code}
* PHP objects are used extensively to show their usefulness for easily passing
information around (such as sql/template objects and error codes) avoiding
procedural functions and clunky parameter passing.
Hopefully this gives you an idea how to setup your applications to work with
Smarty in a way that cleanly separates the application from its presentation.
[Page 1]
[Page 2]
[Page 3]
[Page 4]
[Page 5]
|