Variable usage
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}
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
- Balazs Halasy
- Ricardo Correia Maintainer
- Geir Arne Waaler Maintainer
- Michał Commenter
- Lars Commenter
- Markus Commenter
- Kjartan Commenter
- Adrien Commenter
- Allan Commenter
- Graham Brookins Updated Maintainer
Comments (8 historical)
Written by readers of the earlier documentation sites, kept as they were.
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.
Re: Array inspection
Lars ·
You can use the FOREACH construct.
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.
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
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?
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?
Re: Re: How to modify an array element??
Adrien ·
I mean :
$myArray[$i]++
{set $myArray = $myArray|insert($i,inc($myArray[$i]))|remove(inc($i))}
Re: Re: Re: How to modify an array element??
Allan ·
{$myArray = $myarray|inc()}