PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

{assign}

{assign} is used for assigning template variables during the execution of a template.

Note

Assignment of variables in-template is essentially placing application logic into the presentation that may be better handled in PHP. Use at your own discretion.

Note

See also the short-form method of assigning template vars.

Attributes:

Attribute Name Type Required Default Description
var string Yes n/a The name of the variable being assigned
value string Yes n/a The value being assigned
scope string No n/a The scope of the assigned variable: 'parent','root' or 'global'

Option Flags:

Name Description
nocache Assigns the variable with the 'nocache' attribute

Example 61. {assign}


{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.

  

Example 62. {assign} as a nocache variable


{assign var="name" value="Bob" nocache}
{assign "name" "Bob" nocache} {* short-hand *}

The value of $name is {$name}.

  

The above example will output:


The value of $name is Bob.

  

Example 63. {assign} with some maths


{assign var=running_total value=$running_total+$some_array[$row].some_value}

  

Example 64. {assign} in the scope of calling template

Variables assigned in the included template will be seen in the including template.


{include file="sub_template.tpl"}
...
{* display variable assigned in sub_template *}
{$foo}<br>
...

  

The template above includes the example sub_template.tpl below


...
{* foo will be known also in the including template *}
{assign var="foo" value="something" scope=parent}
{* bar is assigned only local in the including template *}
{assign var="bar" value="value"}
...


Example 65. {assign} a variable to current scope tree

You can assign a variable to root of the current root tree. The variable is seen by all templates using the same root tree.


{assign var=foo value="bar" scope="root"}

  

Example 66. {assign} a global variable

A global variable is seen by all templates.


{assign var=foo value="bar" scope="global"}
{assign "foo" "bar" scope="global"} {* short-hand *}

  

Example 67. Accessing {assign} variables from a PHP script

To access {assign} variables from a php script use getTemplateVars(). 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->getTemplateVars('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->getTemplateVars('foo');

$smarty->assign('foo','Even smarter');

// this will output 'Even smarter'
echo $smarty->getTemplateVars('foo');

?>


The following functions can also optionally assign template variables.

{capture}, {include}, {include_php}, {insert}, {counter}, {cycle}, {eval}, {fetch}, {math}, {textformat}

See also {$var=...}, assign() and getTemplateVars().

Smarty Template Engine