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 1
{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
Balazs Halasy (06/02/2004 1:22 pm)
Balazs Halasy (28/04/2005 2:08 pm)
Comments
def using namespaces
Friday 30 December 2005 8:51:37 am
William Steenbergh
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
Tuesday 03 January 2006 10:51:53 am
Alimi Nabil
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
Friday 09 June 2006 7:39:42 am
Niels
{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
Wednesday 14 June 2006 3:43:23 pm
Stefano Guandalini
The following does not work:
In this case $root:loaded outputs nothing!
g
How to define global variables
Monday 04 September 2006 12:30:09 pm
Leif Arne Storset
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()}
Re: How to define global variables
Wednesday 22 November 2006 10:33:17 am
Olav Bringedal
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.
Re: Re: How to define global variables
Tuesday 25 March 2008 3:58:33 pm
Jon Ramster
This is key point and SHOULD BE EMPHASISED VERY STRONGLY. I had a template calling another sub-template. This sub-template was setting a global variable. This didn't work until I defined the variable before the include, which then gave the variable a value and set its scope as 'global'.
This seems incredibly counter-intuitive to me, but I would love for someone to explain why this is the way it is.
either? what if undefined?
Thursday 16 February 2006 11:50:04 pm
zurgutt
And more importantly, what if variable is previously undeclared?
Re: either? what if undefined?
Wednesday 08 March 2006 11:26:50 pm
AtR
Re: either? what if undefined?
Thursday 07 December 2006 1:00:49 pm
Olav Bringedal
set limitations
Thursday 16 March 2006 5:24:16 pm
Ronnie Garcia
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
Tuesday 19 December 2006 3:00:31 pm
Pike
however, you can achieve something similar with array_merge :
http://ez.no/doc/ez_publish/techn...ence/template_operators/arrays/merge
What is SCOPE?
Monday 19 June 2006 9:50:58 am
vytis