Syntax: {* This is a comment *}
Comments are useful for making internal notes in your template. They are completely ignored in your template file and are invisible to public view (unlike <!-- HTML comments -->).
Synatx: $foo
Variables allow you to dynamically replace the variable by data when the web page is created. For example, instead of writing the record title in the template, you can use a tag like {$title} in place of the title.
Variables can contain numbers, letters and underscores.
You can apply maths to variables that contain numbers. For example:
{$foo+1}
{$foo*$bar}
{$foo->bar-$bar[1]*$baz->foo->bar()-3*7}
Smarty has several different types of variables. The type of the variable depends on what symbol it is prefixed or enclosed within.
Variables in Smarty can be either displayed directly or used as arguments for functions, attributes and modifiers, inside conditional expressions, etc. To print a variable, simply enclose it in the delimiters so that it is the only thing contained between them.
Smarty has many built in functions for formatting, sorting, totalling etc.
You can also use PHP functions (built-in or ones you define in the code) in a Smarty template, for example:
Every Smarty tag either prints a variable or invokes some sort of function. These are processed and displayed by enclosing the function and its attributes within delimiters like so: {funcname attr1="val1" attr2="val2"}.
Smarty allows for:
Built in functions include:
This is used for assigning template variables during the execution of a template.
{assign var="name" value="Bob"}
{assign "name" "Bob"} {* short-hand *}
The value of $name is {$name}.
The above example will output:
The value of $name is Bob.
This is a short-hand version of the {assign} function. For example:
{$name='Bob'}
The value of $name is {$name}.
The above example will output:
The value of $name is Bob.
The {for}{forelse} tag is used to create simple loops. The following different formats are supported:
For examle:
<ul>
{for $foo=1 to 3}
<li>{$foo}</li>
{/for}
</ul>
The above example will output:
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
Another example using MAX attribute.
$smarty->assign('to',10);
<ul>
{for $foo=3 to $to max=3}
<li>{$foo}</li>
{/for}
</ul>
The above example will output:
<ul>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
Example showing use of {forelse}
$smarty->assign('start',10);
$smarty->assign('to',5);
<ul>
{for $foo=$start to $to}
<li>{$foo}</li>
{forelse}
no iteration
{/for}
</ul>
The above example will output:
no iteration
Every {if} must be paired with a matching {/if}. {else} and {elseif} are also permitted.
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.
Qualifier |
Syntax Example |
Meaning |
== |
$a eq $b |
equals |
!= |
$a neq $b |
not equals |
> |
$a gt $b |
greater than |
< |
$a lt $b |
less than |
>= |
$a ge $b |
greater than or equal |
<= |
$a le $b |
less than or equal |
=== |
$a === 0 |
check for identity |
! |
not $a |
negation (unary) |
% |
$a mod $b |
modulous |
is [not] div by |
$a is not div by 4 |
divisible by |
is [not] even |
$a is not even |
[not] an even number (unary) |
is [not] even by |
$a is not even by $b |
grouping level [not] even |
is [not] odd |
$a is not odd |
[not] an odd number (unary) |
is [not] odd by |
$a is not odd by $b |
[not] an odd grouping |
Example {if} statements
{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}
{* parenthesis are allowed *}
{if ( $amount < 0 or $amount > 1000 ) and $volume >= #minVolAmt#}
...
{/if}
{* check for not null. *}
{if isset($foo) }
.....
{/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}
{while} is similar to {if} and takes the same set of modifiers.
Every {while} must be paired with a matching {/while}.
Example {while} loop
{while $foo > 0}
{$foo--}
{/while}
The above example will count down the value of $foo until 1 is reached.
Attributes
Most of the functions take attributes that specify or modify their behavior. Attributes to Smarty functions are much like HTML attributes. Static values don't have to be enclosed in quotes, but it is required for literal strings. Variables with or without modifiers may also be used, and should not be in quotes.
Some attributes require boolean values (TRUE or FALSE). These can be specified as true and false. If an attribute has no value assigned it gets the default boolean value of true.
Example:
{assign var=foo value={counter}}
Loop (repeat) sets of data with the {foreach} syntax.
Conditional statements have the typical if/else structure:
{if $test == "1"}Yes!{else}No!{/if}.
Alternatively:
elseif: {if $person == "Mike"}You are Mike{elseif $person == "Paul"}You are Paul{else}You are neither Mike nor Paul. Who are you?{/if}.
Variable modifiers can be applied to variables, custom functions or strings. To apply a modifier, specify the value followed by a | (pipe) and the modifier name. A modifier may accept additional parameters that affect its behavior. These parameters follow the modifier name and are separated by a : (colon). Also, all php-functions can be used as modifiers implicitly (more below) and modifiers can be combined.
Examples are:
{* 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"}
Created with the Personal Edition of HelpNDoc: Full-featured EBook editor