Skip to the content
Documentation

Variable usage

No ratings yet

Variables are written with a dollar sign, $name, and their names are case sensitive. Some are set by the system before a template runs ($node in a node template, $module_result in the pagelayout); the others you create in the template.

def, set and undef

{def} creates variables, {set} gives an existing variable a new value, and {undef} removes variables. Several variables can be handled in one call:

{def $weather = 'warm'
     $celsius = 32}
The weather is {$weather}: {$celsius} C.
{undef $celsius}
{set $weather = 'cold'}
Now it is {$weather}.
{undef}

The output is "The weather is warm: 32 C. Now it is cold." {undef} without names removes every variable the template defined. Variables a template defines are removed at its end anyway, but an explicit {undef} keeps long templates readable and frees large fetch results early.

Define a variable once per template, then change it with {set}; defining it again, or setting a variable that was never defined, gives a warning in the debug output. {set} changes a variable, never an element of an array or an attribute of an object: build a new array with an operator and set the variable to it:

{def $ids = array( 1, 2 )}
{set $ids = $ids|append( 3 )}
{$ids|implode( ',' )}    {* 1,2,3 *}
{undef $ids}

Details: def, set, undef.

Reading arrays and objects

Elements of a list are read by index, elements of an associative array by key, attributes of an object by name. The dot and the square brackets do the same:

{def $words = array( 'Life', 'is', 'very', 'good!' )
     $parts = hash( 'first', 'Life', 'second', 'is' )}
{$words.0} {$words[2]}           {* Life very *}
{$parts.first} {$parts['second']} {* Life is *}
{$node.object.published|l10n( 'shortdate' )}
{undef $words $parts}

Brackets are needed when the key is in a variable: {$parts[$key]}.

Values that may be missing

is_set() tells whether a variable or key exists, and first_set() returns the first of its parameters that exists:

{if is_set( $view_parameters.color )}...{/if}
{def $heading = first_set( $title, $node.name )}

A template included with {include} can give a parameter a value when the caller passed none, with {default}: {default enable_link=true()}...{/default}, as the standard page_head.tpl does.

Variables for the pagelayout

A variable lives in its template and in the templates it includes. To hand a value from a node template to the pagelayout, for example a page title or extra stylesheets, use the persistent variable: a node template sets it with {set scope='global' persistent_​variable=hash( ... )}, and the pagelayout reads it as $module_​result.​content_​info.​persistent_​variable. It is stored with the view cache, so it also works on cached pages. Other variables set with scope='global' are not stored there and are lost when the view comes from the cache. See The persistent variable and Variables in depth.

In this section

Contributors

Comments (8 historical)

Written by readers of the earlier documentation sites, kept as they were.

  1. Array inspection

    Michał ·

    How can I inspect all values of the array when I do not know its size? There are cases when the array is generated dynamically, and I do not know its size.

    1. Re: Array inspection

      Lars ·

      You can use the FOREACH construct.

    2. Re: Array inspection

      Markus ·

      Yes, "foreach" is the common idea, but you can also use the function "get_type()" to show the length. Here is an example:
      {def $somearray=...}
      {def $length=$somearray|get_type()}
      {for 0 to $length as $counter}
      {$somearray[$counter]}
      {/for}
      {undef}
      This is more helpful if you want to access the neighbour elements of the actual element, for example. If you do not need a counte, "foreach" is easier to use.

  2. Set won`t always work; the Scope parameter

    Kjartan ·

    If you try to use "set" in an inner scope, it will not update a variable outside that scope without the scope-parameter.
    This won`t update the var:
    {def $myVar = "one"}
    {if (#some test that evaluates to true")}
    {set $myVar = "two"}
    This however, will:
    {def $myVar = "one"}
    {if (#some test that evaluates to true")}
    {set $myVar = "two" scope="root"}
    See this page for more info: http://ez.no/doc/ez_publish/techn...nce/template_functions/variables/set
    Hope this saves someone a little time and frustration...
    Cheers,
    Kjartan

  3. How to modify an array element??

    Adrien ·

    Where is the missing part "Changing the contents of array elements"?
    Making a 'set' on an array element destroy the whole array.
    Why there is no cross-link between "/reference/template_operators/arrays/array" and /templates/the_template_language/variable_usage?

    1. Re: How to modify an array element??

      Adrien ·

      Actualy, to have the equivalent to the traditional PHP $myArray[$i]++, I use the following:
      {set $counts = $counts|insert($i,inc($counts[$i]))|remove(inc($i))}
      Huge, isn't it?

      1. Re: Re: How to modify an array element??

        Adrien ·

        I mean :
        $myArray[$i]++
        {set $myArray = $myArray|insert($i,inc($myArray[$i]))|remove(inc($i))}

        1. Re: Re: Re: How to modify an array element??

          Allan ·

          {$myArray = $myarray|inc()}

Comments

Add a comment

Comments are read by a moderator before they appear.