Variables and def
This page is for designers who use variables in templates. It answers: how to create and change them, how long they live, and how to avoid two templates using the same name for different things.
Contents
def, set, undef
From the syntax comment of lib/eztemplate/classes/eztemplatedeffunction.php:
def $i=10 $j=20
def $s1='hello' $s2='world'
set $i=$i|inc
undef $i
undef $s1 $s2{def}creates variables. Defining a variable that already exists assigns the new value and writes the warning "Variable ... is already defined." to the debug output.{set}changes an existing variable.{undef}removes variables;{undef}alone removes every variable this template defined.
Optional parameters: default and first_set
A template that may be included with or without a parameter:
default image_class='medium'sets $image_class only if the caller did not pass it (the kernel's ezimage.tpl does this). In an expression,
first_set( $a, $b, 'fallback' ) returns the first value that is set, and is_set( $x ) / is_unset( $x ) test it.
Scope
A variable defined in a template is visible in templates it includes, not in its caller. To hand a value back up
(for example from a full view to the pagelayout), the kernel uses scope:
set-block scope=root variable=cache_ttl400/set-blockwrites cache_ttl into the root namespace, where the content view reads it. For page-level values, prefer the
persistent variable.
Naming
Included templates share the namespace with their includer: a {def $item ...} in a part overwrites (with a warning) when the
including template already has $item. Habits that prevent it:
- prefix variables of a part with a short tag (
$tz_in a teaser part,pl_as the starter pagelayout does); {undef}at the end of the part what it defined;- pass values to parts as include parameters instead of relying on the includer's names.
Older forms
{let} ... {/let} with {set} inside, and {section}-scoped variables, come from older templates; new templates use
{def}/{undef} (Section (legacy)).
Related pages
- TPL syntax basics
- Include
- Set-block and append-block
- Free documentation: this page is free to copy, translate and improve under the GNU FDL 1.3.
Become an Exponential Partner
Partners share their information, provide funding, contribute back and develop the products with us, and that is how Exponential grows. A partner website and dashboard are planned for mid-2027: please become a partner today to speed up our growth rate and land new features faster.