What is Smarty?
Why use it?
Use Cases and Work Flow
Syntax Comparison
Template Inheritance
Best Practices
Crash Course
You may use the Smarty logo according to the trademark notice.
For sponsorship, advertising, news or other inquiries, contact us at:
The PHP programming language is great for code development, but many will argue that embedding its terse statement syntax into HTML can be difficult to manage. Smarty makes up for this by insulating PHP from the presentation with a much simpler tag-based syntax. The tags reveal application content within the presentation in a clean, intuitive manner. No PHP knowledge is required to manage Smarty templates.
To demonstrate, let's compare PHP and Smarty syntax. We'll start with a simple variable, using PHP's short-tag syntax.
<?=$foo?>
{$foo}
Note that the PHP syntax uses 5 punctuation characters to display a simple variable: <?=?>, whereas Smarty uses 2: {}. (We ignore the $ since it is always present in both syntaxes.) Now let's compare array access:
<?=$foo['bar']?>
{$foo.bar}
PHP uses 9 punctuation chars, and Smarty uses 3. Even with this simple example, you can begin to see how the syntax noise is greatly reduced by Smarty. Moving on, let's try a foreach loop:
<?php foreach($foo as $bar): ?> ... <?php endforeach; ?>
{foreach $foo as $bar} ... {/foreach}
PHP uses 18 punctuation chars, and Smarty uses only 5. Now let's try some real-world use, mixing things with HTML tags:
<?php if(!empty($foo)): ?> <?php foreach($foo as $bar): ?> <a href="<?=$bar['zig']?>"><?=$bar['zag']?></a> <a href="<?=$bar['zig2']?>"><?=$bar['zag2']?></a> <a href="<?=$bar['zig3']?>"><?=$bar['zag3']?></a> <?php endforeach; ?> <?php else: ?> There were no rows found. <?php endif; ?>
{foreach $foo as $bar} <a href="{$bar.zig}">{$bar.zag}</a> <a href="{$bar.zig2}">{$bar.zag2}</a> <a href="{$bar.zig3}">{$bar.zag3}</a> {foreachelse} There were no rows found. {/foreach}
In the short example above, Smarty saved 110 characters, or a 36% syntax reduction. Also notice that Smarty takes care of boilerplate functions wherever it can, such as the empty() test in the above example. Smarty cleans up the syntax of templates, making them less fragile.
Notice we have been using the PHP short-tag syntax <?=$foo?> for variable output. This is used in PHP as as shortcut to the longer syntax of <?php echo $foo; ?>. This helps shorten things up bit, but you still don't gain much in readability. Many PHP devs will tell you to avoid short-tags anyway, as this defeats cross-system compatible code since short-tags are optional. Lets compare the long syntax to a Smarty variable:
<?php echo $foo; ?>
{$foo}
Even with this short example, you can see a 13 character savings (69% shorter) to display the same thing. Now let's see this mixed in with some HTML:
<a href="<?php echo $bar['zig']; ?>"><?php echo $bar['zag']; ?></a> <a href="<?php echo $bar['zig2']; ?>"><?php echo $bar['zag2']; ?></a> <a href="<?php echo $bar['zig3']; ?>"><?php echo $bar['zag3']; ?></a>
<a href="{$bar.zig}">{$bar.zag}</a> <a href="{$bar.zig2}">{$bar.zag2}</a> <a href="{$bar.zig3}">{$bar.zag3}</a>
Another issue with PHP tags is that they share the <> characters of HTML tags. In the application code this isn't an issue, but mixed with HTML, <?php ?> tags are a maddening process to tell them apart from the HTML tags. This also blurs the line between application and presentation separation since any PHP logic can be injected into a template. This is where a simple {tag} syntax is a huge welcome. Smarty keeps the presentation short and concise, well-defined, less error prone and easy to maintain.
Smarty features simplified, English-reading logic statements. The following example displays the odd numbers of 1-20 (1,3,5,7,...):
<?php for($x=0; $x<20; $x+=2): ?> <php echo $x+1; ?> <?php endfor; ?>
{for $x = 1 to 20 step 2} {$x} {/for}
Which is easier to understand? How about for the non-developer types?
The following example loops over the $foo array and prints the value in a table cell. Every 4th row it prints </tr><tr> to start a new table row:
<?php $counter=0; ?> <?php foreach($foo as $bar): ?> <?php if(($counter+1) % 4 == 0): ?> </tr><tr> <?php endif; ?> <td><?php echo $bar; ?></td> <?php $counter++; ?> <?php endforeach; ?>
{foreach $foo as $bar} {if $bar@iteration is div by 4} </tr><tr> {/if} <td>{$bar}</td> {/foreach}
The next example lower-cases and html-escapes a variable:
<?php echo htmlspecialchars(strtolower($foo),ENT_QUOTES,'UTF-8'); ?>
Smarty (notice the natural left-to-right statement flow):
{$foo|lower|escape}
lower and escape are two of many built-in plugins that come with Smarty. They contain powerful features aimed specifically at presentational output. Do you want to javascript-escape the variable instead? One simple parameter:
{$foo|lower|escape:javascript}
Which of the above do you think a designer would rather follow? Short, concise syntax is key to fast deployment and easy maintenance.
These are just a few examples of how Smarty saves you time managing templates, insulates templates from PHP code, and makes template maintenance easy, even for the completely inexperienced user. See also All about Smarty, Use Cases and Work Flow, Template Inheritance and Why use Smarty.