assign

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

Using function:
<?php
assign(mixed $value, string $var)
  • value: the value that you want to save
  • var: the variable name (without the leading $)
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.
Example #1

    {assign 'test string' myVar}
    Variable contains: {$myVar} 
    

The above example will output:

Variable contains: test string
Example #2

    {assign name="myVar" value="test string"}
    Variable contains: {$myVar} 
    

The above example will output:

Variable contains: test string
Example #3

    {$name="Bob"}
    What is your name?
    My name is {$name}.  
    

The above example will output:


    What is your name?
    My name is Bob.      
    
Example #4 with some maths

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

    OR

    {assign var=amount value=((10+20)/2)}
    Variable contains: {$amount} 

    OR

    {assign ((10+20)/2) amount}
    Variable contains: {$amount} 

    OR

    {$amount=((10+20)/2)}
    Variable contains: {$amount}