Real world smarty templating

Tuesday 15th May 2007 10:03 PM

In a previous blog about caching pages, it was suggested I use smarty... well I decided to go for it, and while people have been using it for a long time now, I'm very impressed. Here's a few tips for people looking to get a kick start into its syntax and functions.

This is not meant to be an exhaustive article, and you'll find some much more indepth info at the official smarty site. Through my experience working with it, these are some useful real world examples and theory that should be useful to anyone tackling a smarty templated site!


If Statements:

If statements are great if you want a simple condition in your template (the else is optional).

{if $id == 1}
    ID is equal to 1
{else}
    ID is something else.
{/if}


Constants:

Smarty can access PHP Constants you've defined with a simple: {$smarty.const.SITENAME} in your template (replace SITENAME with your constant).


Foreach Loops:

Much the same as in native PHP, just assign an array to a variable, and let smarty loop through it for you. For example, in your PHP code:

$myArray = array(
  array( 'id' => 1, 'name' => 'Jim'),
  array( 'id' => 2, 'name' => 'Frank'),
);

$smarty->assign('myArray', $myArray);

And to loop through this in a smarty template (again, the foreachelse is optional)...

{foreach item=row from=$allArray name=people}
  Name: {$row.name} ... ID: {$row.id} <br />
{foreachelse}
  <p>No Users Found</p>
{/foreach}

Smarty keeps some internal variables you can access inside the foreach loop, such as an internal count of rows, and the ability to select first/last rows and find total counts.


Alternating Rows:

This is a very useful tip for displaying rows of data with alternating colours with the inbuilt cycle function (using CSS classes here).

{foreach item=row from=$allArray}
    <tr class="{cycle values="row1,row2"}">
      <td>{$row.name}</td><td>{$row.id}</td>
    </tr>
{/foreach}


Classes and Methods:

Smarty is more than just simple foreaches and if's, you can assign an object to be available in your templates, and then use its methods from within your template. (I use assign_by_ref as it feels more natural, but you can also use register_object for the same effect).

$smarty->assign_by_ref('myObj', $myObj);

In your template...

{$myObj->doSomething('test', 1)}


Modifiers:

Modifiers are fairly basic, so there's not really any need to go to far into them.

{$name|capitalize} will make the variable '$name' transformed to all capitals, and something like {$myDate|date_format:'%D'} can format a date in any way you like.

You can also use regular PHP functions, e.g. {if 'name'|in_array:$myArray}Found{/if}


Smarty Special Variables:

Smarty provides some easy and quick ways to access some useful variables such as post, get, cookies and session. For example:

{$smarty.post.variableName}
{$smarty.session.variableName}

Also, the very useful {$smarty.now} will return the current date, you can format it as mentioned above using a modifier.

Comments on this article:


I love feedback and comments, be the first!

Add Comment:


Make a Comment

*Nb, all comments are moderated to prevent spam or inappropriate content.








netforge logo
netforge provides high quality and friendly website design services to business. We're Australian based and reliable... (find out more).