Smarty 3 Overview
Rewritten for PHP 5
The Smarty 3 code base is a 100% rewrite, and contains many language features that do not exist in PHP 4. This means Smarty 3 will NOT work with PHP 4. If you need PHP 4 support, you will have to stick with Smarty 2 until you can upgrade PHP.
Design Goal
Smarty's fundamental design goal remains the same: To separate application code from the presentation. This implies that PHP is separated from the presentation. A detailed explanation of why this is important can be found here.
New Lexer/Parser
Smarty has a new template parser, a real syntax lexer. This gives Smarty much finer control over its template syntax. Things like in-template math, line-precision error messages and recursive template functions are now possible.
Template Objects
You can now make template objects and execute them independently. Example:
PHP
$tpl = $smarty->createTemplate('my.tpl'); $tpl->assign('foo','bar'); $smarty->display($tpl); // or $tpl->display();
Data Objects
The variables assigned to a template can now be managed independently as a Smarty_Data object. Example:
PHP
$data = new Smarty_Data; $data->assign('foo','bar'); $smarty->display('my.tpl',$data); $tpl = $smarty->createTemplate('my.tpl',$data);
PHP Streams
You can now use PHP Streams as data sources for components of Smarty. If you call:
PHP
$smarty->display('foo:bar.tpl');
or in a template call:
Smarty
{include file="foo:bar.tpl"}
Smarty will first look for a registered template resource named foo. If nothing is found, it will check if a PHP stream is available. If foo://bar is available, Smarty will use it to fetch the template. You can also use streams to call variables. {$foo:bar} will use the foo://bar stream to get the template variable.
Template Inheritance
Templates can now inherit from each other. You mark blocks of a template with {block name=foo}{/block} tags, a template can inherit with the {extend file="my.tpl"} tag, and then you can replace, change or append the blocked contents from the inheriting template(s). Example:
parent .tpl
<html> <head> <title>{block name=title}default title{/block}<title> </head> <body> {block name=body}default body{/block} </body> </html>
child .tpl
{extends file="parent.tpl"} {block name=title}My Child Title{/block} {block name=body}My Child Body{/block}
output of $smarty->display('child.tpl');
<html> <head> <title>My Child Title<title> </head> <body> My Child Body </body> </html>
See more here.
"{" and "}" (Javascript) Auto-Escapement
In Smarty 2, you had to escape occurances of {} characters by surrounding them with {literal}{/literal} blocks or replacing them with {ldelim}{rdelim} tags. With Smarty 3, the braces will be ignored so long as they are surrounded by white space.
javascript
<script> // the following braces are ignored by Smarty // since they are surrounded by whitespace function foobar { alert('foobar!'); } // this one will need literal escapement {literal} function bazzy {alert('foobar!');} {/literal} </script>
Variable Filters
The default template modifier feature of Smarty 2 has been replaced with variable filters. A variable filter can be applied to the output of template variables globally. We have pre, post, and output filters, now we have a fourth choice: 'variable'. When you apply the filter such as:
PHP
$smarty->registerFilter('variable','htmlspecialchars');
then all output from variables is HTML escaped. You can also selectively disable this in-line with {$foo nofilter}.
In-Template Function Definitions
You can create functions within a template and call them just like a plugin function. When you have a plugin that generates presentational content, keeping it in the template is often a more manageable choice. It also simplifies data traversal, such as deeply nested menus. Example:
Smarty
{* define the function *} {function name=menu level=0} <ul class="level{$level}"> {foreach $data as $entry} {if is_array($entry)} <li>{$entry@key}</li> {menu data=$entry level=$level+1} {else} <li>{$entry}</li> {/if} {/foreach} </ul> {/function} {* create an array to demonstrate *} {$menu = ['item1','item2','item3' => ['item3-1','item3-2','item3-3' => ['item3-3-1','item3-3-2']],'item4']} {* run the array through the function *} {menu data=$menu}
Output
* item1 * item2 * item3 o item3-1 o item3-2 o item3-3 + item3-3-1 + item3-3-2 * item4
Precision Caching
You can now specify items to be non-cached inline such as {$foo nocache} or {include file="foo.tpl" nocache}
Backward-Compatibility with Smarty 2
Much of Smarty 3 is backward compatible with Smarty 2, except for the items noted in the SMARTY2_BC_NOTES file included with the Smarty 3 distribution. Please see that file for a detail breakdown of BC differences.