Re: Re: def using namespaces (2)
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
Comments (3 historical)
Written by readers of the earlier documentation sites, kept as they were.
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()}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.
Re: Re: How to define global variables
Jon Ramster ·
Thank you, thank you, thank you. Very, very frustrating until I came across this point.
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.