Get Smarty

Donate

Paypal

Smarty Icon

You may use the Smarty logo according to the trademark notice.

Smarty Template Engine Smarty Template Engine

For sponsorship, advertising, news or other inquiries, contact us at:

Sites Using Smarty

Advertisement

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.

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]

Advertisement

Sponsors [info]

Sponsors