Templating > Smarty basics

Smarty basic syntax is available online.

Comments

{* this is a comment *}

Template comments are surrounded by asterisks, which are then surrounded by the delimiter tags like so:

Smarty comments are not displayed in the final output of the template. They are used for making internal notes in the templates.

Variables

Template variables start with a dollar sign. They can contain numbers, letters and underscores, much like a PHP variable. You can reference arrays that are indexed numerically or non-numerically. You can also reference object properties and methods. Config file variables are an exception to the dollar sign syntax. They can be referenced with surrounding hashmarks, or with the special $smarty.config variable.

{$foo} displays a simple variable (non array/object)
{$foo[4]} displays the 5th element of a zero-indexed array
{$foo.bar} displays the "bar" key value of an array
{$foo.$bar} displays the variable key value of an array
{$foo->bar} displays the object property "bar"
{$foo->bar()} displays the return value of object method "bar"

Many other combinations are allowed:

{$foo.bar.baz}
{$foo.$bar.$baz}
{$foo[4].baz}
{$foo[4].$baz}
{$foo.bar.baz[4]}
{$foo->bar($baz,2,$bar)} <-- passing parameters
{"foo"} <-- static values are allowed

Variable modifiers

Variable modifiers can be applied to variables, custom functions or strings. To apply a modifier, specify the value followed by the | (pipe) and the modifier name. A modifier may accept additional parameters that affect its behavior. These parameters follow the modifer name and are separated by: (colon).

{* apply modifier to a variable *}
{$title|upper}
{* modifier with parameters *}
{$title|truncate:40:"..."}
{* apply modifier to a function parameter *}
{html_table loop=$myvar|upper}
{* with parameters *}
{html_table loop=$myvar|truncate:40:"..."}
{* apply modifier to literal string *}
{"foobar"|upper}
{* using date_format to format the current date *}
{$smarty.now|date_format:"%Y/%m/%d"}
{* apply modifier to a custom function *}
{mailto|upper address="me@domain.dom"}

If you apply a modifier to an array variable instead of a single value variable, the modifier will be applied to every value in that array. If you really want the modifier to work on an entire array as a value, you must prepend the modifier name with an @ symbol like so: {$articleTitle|@count} (this will print out the number of elements in the $articleTitle array.)

You can apply any number of modifiers to a variable. They will be applied in the order they are combined, from left to right. They must be separated with a | (pipe) character.

{$articleTitle|upper|spacify}
{$articleTitle|lower|spacify|truncate}
{$articleTitle|lower|truncate:30|spacify}
{$articleTitle|lower|spacify|truncate:30:". . ."}

if, elseif, else

{if} statements in Smarty have much the same flexibility as PHP if statements, with a few added features for the template engine. Every {if} must be paired with an {/if}. {else} and {elseif} are also permitted. All PHP conditionals are recognized, such as ||, or, &&, and, etc.

The following is a list of recognized qualifiers, which must be separated from surrounding elements by spaces. Note that items listed in [brackets] are optional. PHP equivalents are shown where applicable.

{if $name eq "Fred"}
 Welcome Sir.
{elseif $name eq "Wilma"}
 Welcome Ma'am.
{else}
 Welcome, whatever you are.
{/if}
{* an example with "or" logic *}
{if $name eq "Fred" or $name eq "Wilma"}
 ...
{/if}
{* same as above *}
{if $name == "Fred" || $name == "Wilma"}
 ...
{/if}
{* the following syntax will NOT work, conditional qualifiers
 must be separated from surrounding elements by spaces *}

{if $name=="Fred" || $name=="Wilma"}
 ...
{/if}
{* parenthesis are allowed *}
{if ( $amount < 0 or $amount > 1000 ) and $volume >= #minVolAmt#}
 ...
{/if}
{* you can also embed php function calls *}
{if count($var) gt 0}
 ...
{/if}
{* test if values are even or odd *}
{if $var is even}
 ...
{/if}
{if $var is odd}
 ...
{/if}
{if $var is not odd}
 ...
{/if}
{* test if var is divisible by 4 *}
{if $var is div by 4}
 ...
{/if}
{* test if var is even, grouped by two. i.e.,
0=even, 1=even, 2=odd, 3=odd, 4=even, 5=even, etc. *}

{if $var is even by 2}
 ...
{/if}
{* 0=even, 1=even, 2=even, 3=odd, 4=odd, 5=odd, etc. *}
{if $var is even by 3}
 ...
{/if}>

foreach, foreachelse

foreach loops are an alternative to section loops. foreach is used to loop over a single associative array.

The syntax for foreach is much easier than section, but as a tradeoff it can only be used for a single array. {foreach} tags must be paired with {/foreach} tags. Required parameters are from and item.

The name of the foreach loop can be anything you like, made up of letters, numbers and underscores. foreach loops can be nested, and the nested foreach names must be unique from each other.

The from variable (usually an array of values) determines the number of times foreach will loop.

{foreachelse} is executed when there are no values in the from variable.

{* this example will print out all the values of the $custid array *}
{foreach from=$custid item=curr_id}
 id: {$curr_id}<br>
{/foreach}
OUTPUT:
id: 1000<br>
id: 1001<br>
id: 1002<br>
Example 7-5. foreach key
{* The key contains the key for each looped value
assignment looks like this:
$smarty->assign("contacts", array(array("phone" => "1", "fax" => "2", "cell" => "3"),
 array("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234")));
*}

{foreach name=outer item=contact from=$contacts}
 {foreach key=key item=item from=$contact}
 {$key}: {$item}<br>
 {/foreach}
{/foreach}
OUTPUT:
phone: 1<br>
fax: 2<br>
cell: 3<br>
phone: 555-4444<br>
fax: 555-3333<br>
cell: 760-1234<br>

Foreach-loops also have their own variables that handle foreach properties. These are indicated like so: {$smarty.foreach.foreachname.varname} with foreachname being the name specified as the name attribute of foreach.