set
Summary
Sets the value of a variable.
Usage
{set $var1=value1 [ var2=value2 [...] ] [ name=name ] [ scope=scope ]}Parameters
| Name | Type | Description | Required |
|---|---|---|---|
| var1 | string | Name of variable number one (with a dollar sign in front of it). | Yes. |
| value1 | any | The value that should be assigned to variable 1. | Yes. |
| var2 | string | Name of variable number two (with a dollar sign in front of it). | No. |
| value2 | any | The value that should be assigned to variable 2. | No. |
| name | string | The name of the target namespace. | No. |
| scope | string | The scope ("global", "root" or "relative"). | No. |
Description
This function makes it possible to assign new values to variables that previously have been declared using either the "def" function. The "name" and "scope" parameters are optional and can be used to set the desired namespace and scope.
Examples
Example 1
{def $apples=4}
Before: {$apples} <br/>
...
{set $apples=8}
After: {$apples} <br/>The following output will be produced:
Before: 4
After: 8
Example 2
{def name=ns1 $var1='ns1 org value'}
{def name=ns2 $var1='ns2 org value'}
Original values: <br/>
$ns1:var1 : {$ns1:var1} <br/>
$ns1:ns2:var1 : {$ns1:ns2:var1} <br/>
...
{set name=ns1 scope=root var1='new value'}
{set var1='new value'}
...
New values: <br />
$ns1:var1 : {$ns1:var1}<br/>
$ns1:ns2:var1 : {$ns1:ns2:var1}<br/>The following output will be produced:
Original values:
$ns1:var1 : ns1 org value
$ns1:ns2:var1 : ns2 org value
New values:
$ns1:var1 : new value
$ns1:ns2:var1 : new value
Example 3
{def $today='rainy'}
Before: Today is a {$today} day!
{set scope=global $today='sunny'}
After: Today is a {$today} day!The following output will be produced:
Before: Today is a rainy day!
After: Today is a sunny day!
Contributors
- Balazs Halasy Maintainer
- Ricardo Correia Maintainer
- William Steenbergh Commenter
- Alimi Nabil Commenter
- Niels Commenter
- Stefano Guandalini Commenter
- Leif Arne Storset Commenter
- Olav Bringedal Commenter
- Guillaume Commenter
- Gaetano Commenter
- zurgutt Commenter
- AtR Commenter
- Ronnie Garcia Commenter
- Pike Commenter
- vytis Commenter
Comments (14 historical)
Written by readers of the earlier documentation sites, kept as they were.
def using namespaces
William Steenbergh ·
Hi eZ Pub guys,
In the manual for {def} it is stated that def does not support the 'name' and 'scope' parameters. In the examples of {set} however, it does. Did you simply 'search and replace' all occurences of {let} in the documentation, or do some parts of the manual need an update?
Re: def using namespaces
Alimi Nabil ·
{set} is not {def} ;).
Namespaces are supported by the set function but not by the def statement as mentionned in the documentation.
In other words, you can define a variable and set a value for a specific namespace.
Re: Re: def using namespaces
Niels ·
Example 1:
{def name=ns1 $var1='ns1 org value'}
{def name=ns2 $var1='ns2 org value'}
There still ist a name=.. in there.
Re: Re: def using namespaces
Stefano Guandalini ·
Yes, but what if I want to define a variable in the 'root' or 'global' namespace?
The following does not work:
{def $loaded='pippopippo'} {set name=root $loaded='pippopippo'}In this case $root:loaded outputs nothing!
g
How to define global variables
Leif Arne Storset ·
After some forum searching, trial and failure, I found out how to work with global variables. Apparently, 'root' means current template, and 'global' is for exchanging data between templates. :S
You cannot declare a variable as global, so just declare it like a regular variable (definition is compulsory):
{def $total_items=0}To define or set a global variable:
{set scope='global' $total_items=0}To read a global variable:
{$#total_items}To update a global variable, you naturally combine the above:
{set scope='global' $total_items=$#total_items|inc()}Replying to Leif Arne Storset
Re: How to define global variables
Olav Bringedal ·
Thanks for your research.
It really logical, but I had to try a few times before it got to me:
You need to declare the variable in pagelayout.tpl to access it in templates on same or higher level than the one that sets it.
Replying to Olav Bringedal
Re: Re: How to define global variables
Guillaume ·
This doesn't work for me, i can set the var but i cannot get the value.
Replying to Guillaume
Re: Re: Re: How to define global variables
Gaetano ·
If you have trouble using scopes or namespaces, a simpler solution is to use the "persistent_variable" var:
- it is accessed as $persistent_variable in node templates
- it can be set directly in any template without having to be def()ined first
- it can be set() in an included template and it will be available in the including template
- it will even be available in another template included after the one where it is set
- it will be available in the pagelayout via $module_result.content_info.persistent_variable
the only drawback is that it is not easy to use in custom modules.
To store multiple values, just make it a hashmap containing all the desired values.
Otoh if all you need to pass around between templates are strings, you might also investigate the set-block template function, which takes scope and namespace params. It works better than set.
either? what if undefined?
zurgutt ·
"..variables that previously have been declared using either the "def" function." - either def or what?
And more importantly, what if variable is previously undeclared?
Re: either? what if undefined?
AtR ·
{if is_set($module_result.node_id)}
Re: either? what if undefined?
Olav Bringedal ·
"either def or predefined"?
set limitations
Ronnie Garcia ·
According to this bug report : http://ez.no/community/bugs/weird_behavior_or_bug_on_set_statement
You can't {set} array values or object attributes.
Eg: {set $object_parameters['link_to_image']=true()}
is *not* valid (at least in 3.6.2). Bummer.
Re: set limitations
Pike ·
true, and it's not considered a bug and the issue is closed. the rule is: the set function can only have variable names at the left hand side of the "=".
however, you can achieve something similar with array_merge :
http://ez.no/doc/ez_publish/techn...ence/template_operators/arrays/merge
What is SCOPE?
vytis ·
what mean "scope"? Does "global" mean, that this variable will known in other templates?