6 Killer Smarty Features You Need to Know

Smarty is by far my favorite PHP template engine. I’ll debate the pros and cons of using a template engine in another post, but I wanted to share some of the Smarty features that I didn’t know existed until I dug through the manual.

1. {cycle}

{cycle} is definitely my favorite Smarty command. You pass it a string of comma-separated values and it cycles through them. I mostly use this, as the manual demonstrates, for zebra striping tables or comments or any sort of list.

{section name=rows loop=$data}
<tr bgcolor="{cycle values="#eeeeee,#d0d0d0"}">
   <td>{$data[rows]}</td>
</tr>
{/section}

2. {foreachelse}

In php, I normally do things like if count(array) > 0 then foreach the array. This essentially uses two statements and messes up my code. Smarty takes care of this for me with {foreachelse}:

{foreach key=cid item=con from=$results}
    <a href="contact.php?contact_id={$cid}">{$con.name} - {$con.nick}</a><br />
{foreachelse}
    No items were found in the search
{/foreach}

3. {insert}

Smarty’s caching mechanism is one of it’s best features. Caching your pages can really cut down on your page’s computation time and speed things up. Normally, even if only a small portion of your page is dynamic, you can’t cache the whole page. Smarty takes care of that however with the {insert} function. They work like normal {include} functions (which work just like PHP’s include() function) except they do not cache the included file, even when the rest of the template is cached.

4. {counter}

Before I realized there was a {counter} function, I would use the {assign} function to do x = x + 1. {counter} handles all of that for you with a number of other great options including direction (count up/down), skip, start, print and assign. Extremely useful for any time where you need to create a numbered list out of a database or API call.

{counter start=0 skip=2}<br />
{counter}<br />
{counter}<br />
{counter}<br />

will output:

0<br />
2<br />
4<br />
6<br />

5. {html_table}

You have to use tables sometimes. {html_table} is an easy method to output the HTML for the table without using a {foreach} and inserting all the HTML yourself. For example, this PHP:

$smarty->assign( 'data', array(1,2,3,4,5,6,7,8,9) );
$smarty->assign( 'tr', array('bgcolor="#eeeeee"','bgcolor="#dddddd"') );

and this Smarty:

{html_table loop=$data cols="first,second,third,fourth" tr_attr=$tr table_attr='border="0"'}

will generate this HTML:

<table border="0">
<thead>
<tr>
<th>first</th><th>second</th><th>third</th><th>fourth</th>
</tr>
</thead>
<tbody>
<tr bgcolor="#eeeeee"><td>1</td><td>2</td><td>3</td><td>4</td></tr>
<tr bgcolor="#dddddd"><td>5</td><td>6</td><td>7</td><td>8</td></tr>
<tr bgcolor="#eeeeee"><td>9</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
</tbody>
</table>

6. The Debugging Console

When building a large app or working with an app I didn’t build (more likely), figuring out what variables are available and what there values are can be a real pain. Rather than sticking a bunch of echo or var_dump commands in the PHP, I simply called up the Smarty debugging console. The debugging console uses Javascript to popup a window that shows you the names of all the included templates and the assigned variables for the current page.

There are three options for calling up the console.

  1. Set the $debugging config variable to TRUE. The console is always shown when this variable is turned on. Useful when you’re in the middle of development.
  2. Set the $debugging_ctrl config variable to TRUE. In this case, simply add SMARTY_DEBUG in the query string of the page you want to debug. This is useful when you want to debug a live site.
  3. Use {$debug} inside a template when you want to debug just one template. Useful when extending a live site or editing just one template.

Did this post help you? Did you know it all already? What else do you like/dislike about Smarty?

  1. What Do You Think?