ZPT Basics (part 2) - Switching Things Around (
Page 5 of 9 )
Now that you know how variables work, it's time to examine the TAL
"condition" attribute, which lets you add decision-making capabilities
to your templates. In this section, I'll be examining conditional
expressions and comparison operators, and demonstrating, with practical
examples, how they can be used to craft more intelligent code.
Let's begin with a simple example:
<span tal:define="global switch string:on"></span>
<span tal:condition="switch">You left the light switch on</span>
In this case, the visibility of the second <span> depends on the
presence or absence of the "switch" variable. If the variable is
present, the second <span> will be displayed; if not, it will remain
hidden.
All this happens via the special TAL "condition" attribute, which,
according to the TAL specification, is used to "...include a particular
part of a Template only under certain conditions, and omit it
otherwise". This TAL attribute is thus analogous to the traditional "if"
statement found in all programming languages, and it can come in very
handy when you need to add control flow routines to your templates.
The expression provided to the "condition" attribute must be a valid
TALES expression, which evaluates to either true or false.