Smarty Array Functions
Multidimensional arrays are accessible within a template using the dot syntax:
/* PHP code */
$array = array (array('name' => 'John', 'age' => 35),
array('name' => 'Jane', 'age' => 42));
$page->assign('people', $array);
{* template *}
{foreach from=$people item=person}
{$person.name} is {$person.age} years old.
{/foreach}
The "foreachelse" function can be used if an array has no items in it (and therefore the loop is never executed):
{foreach from=$array item=v}
{$v}
{foreachelse}
There are no items to print.
{/foreach}
Using PHP Functions in Smarty
The "php" function allows you to place executable PHP code within a template. This may not be advisable, though, as this breaks the distinction between application code and presentation.
Any block put within {literal}{/literal} tags will not be touched by Smarty. This is particularly useful for JavaScript and CSS, whose frequent use of the curly braces would otherwise confuse the Smarty template engine:
{literal}
{/literal}
The "strip" function removes newlines and whitespace from enclosed template text. Conversely, the "strip" modifier removes whitespace from variable values.
Creating Custom Smarty Functions
Smarty has many custom functions and more can be added by creating your own plugin. See the Smarty manual for the complete list of custom functions.
|