To access {assign} variables from a php script use get_template_vars(). Here's the template that creates the variable $foo.
{assign var='foo' value='Smarty'}
The template variables are only available after/during template execution as in the following script.
<?php
// this will output nothing as the template has not been executed echo $smarty->get_template_vars('foo');
// fetch the template to a variable
$whole_page = $smarty->fetch('index.tpl');
// this will output 'smarty' as the template has been executed echo $smarty->get_template_vars('foo');
$smarty->assign('foo','Even smarter'); // this will output 'Even smarter' echo $smarty->get_template_vars('foo'); ?>
The following functions can also optionally assign template variables.
{capture}, {include}, {include_php}, {insert}, {counter}, {cycle}, {eval},
{fetch}, {math}, {textformat}
See also assign() and get_template_vars().
{counter}
{counter} is used to print out a count. {counter} will remember the count on each iteration. You can adjust the number, the interval and the direction of the count, as well as determine whether or not to print the value. You can run multiple counters concurrently by supplying a unique name for each one. If you do not supply a name, the name “default” will be used.
If you supply the assign attribute, the output of the {counter} function will be assigned to this template variable instead of being output to the template.
Attribute Name Type Required Default Description
name string No default The name of the
Attribute Name Type Required Default Description
start number No 1 The initial number
to start counting from
skip number No 1 The interval to
count by
direction string No up The direction to
count (up/down) print boolean No TRUE Whether or not to
print the value
assign string No n/a the template
variable the output will be assigned to
Example 8.4. {counter}
{* initialize the count *} {counter start=0 skip=2}<br /> {counter}<br />
{counter}<br /> {counter}<br />
this will output:
0<br /> 2<br /> 4<br /> 6<br />
{cycle}
{cycle} is used to alternate a set of values. This makes it easy to for example, alternate between two or more colors in a table, or cycle through an array of values.
Attribute Name Type Required Default Description
name string No default The name of the
cycle
values mixed Yes N/A The values to cycle
through, either a comma delimited
Attribute Name Type Required Default Description
attribute), or an array of values print boolean No TRUE Whether to print the
value or not advance boolean No TRUE Whether or not to
advance to the next value
delimiter string No , The delimiter to
use in the values attribute
assign string No n/a The template
variable the output will be assigned to reset boolean No FALSE The cycle will be set
to the first value and not advanced • You can {cycle} through more than one set of values in a template by supplying a name attribute.
Give each {cycle} an unique name.
• You can force the current value not to print with the print attribute set to FALSE. This would be useful for silently skipping a value.
• The advance attribute is used to repeat a value. When set to FALSE, the next call to {cycle} will print the same value.
• If you supply the assign attribute, the output of the {cycle} function will be assigned to a template variable instead of being output to the template.
Example 8.5. {cycle}
{section name=rows loop=$data}
<tr class="{cycle values="odd,even"}"> <td>{$data[rows]}</td>
</tr> {/section}
The above template would output:
<tr class="odd"> <td>1</td> </tr> <tr class="even"> <td>2</td> </tr> <tr class="odd"> <td>3</td> </tr>
{debug}
{debug} dumps the debug console to the page. This works regardless of the debug settings in the php script. Since this gets executed at runtime, this is only able to show the assigned variables; not the templates that are in use. However, you can see all the currently available variables within the scope of a template.
Attribute Name Type Required Default Description
output string No javascript output type, html or javascript
See also the debugging console page.
{eval}
{eval} is used to evaluate a variable as a template. This can be used for things like embedding template tags/variables into variables or tags/variables into config file variables.
If you supply the assign attribute, the output of the {eval} function will be assigned to this template variable instead of being output to the template.
Attribute Name Type Required Default Description
Attribute Name Type Required Default Description
assign string No n/a The template
variable the output will be assigned to
Technical Note
• Evaluated variables are treated the same as templates. They follow the same escapement and security features just as if they were templates.
• Evaluated variables are compiled on every invocation, the compiled versions are not saved! However if you have caching enabled, the output will be cached with the rest of the template.
Example 8.6. {eval}
The contents of the config file, setup.conf.
emphstart = <strong> emphend = </strong>
title = Welcome to {$company}'s home page!
ErrorCity = You must supply a {#emphstart#}city{#emphend#}. ErrorState = You must supply a {#emphstart#}state{#emphend#}.
Where the template is:
{config_load file='setup.conf'} {eval var=$foo}
{eval var=#title#} {eval var=#ErrorCity#}
{eval var=#ErrorState# assign='state_error'} {$state_error}
The above template will output:
This is the contents of foo.
Welcome to Foobar Pub & Grill's home page! You must supply a <strong>city</strong>. You must supply a <strong>state</strong>.