ZPT Basics (part 2) - A Question Of Scope (
Page 4 of 9 )
If you look closely at all the examples on the previous page, you will
see that I've defined and used every variable within the same element.
This isn't really very practical - it's unlikely that you'll have the
luxury of using a variable only at the time of definition in real-world
development.
So the question you should be asking is, can a variable defined in one
particular element be used in other elements too? Let's find out.
<span tal:define="name string:Bond, James Bond"></span>
Now, who did you say you were again?
<b tal:content="string:$name">name here again.</b>
In this code snippet, I've defined the variable "name" in one HTML
element, but used it in another. And when I attempt to have Zope render
this page for me, it barfs all over the screen...
Does this mean that variables can only be accessed within the same
element? I thought so...until a quick read of the TAL documentation
clarified things.
You see, in TAL, variables exist by default only within the local scope
- the element in which they have been defined, and all the children of
that element. In the example above, the <b> element is not a child of
the <span> element containing the variable definition - which is why
Zope can't find it.
Now, if I were to rewrite the code above to make the <b> element a child
of the <span> element,
<span tal:define="name string:Bond, James Bond">
Now, who did you say you were again?
<b tal:content="string:$name">name here again.</b>
</span>
things would work a lot better.
Obviously, while this is certainly better than having a Zope error
screen thrown at you, it's not really an optimal solution. Which is why
TAL also offers the "global" keyword...
<span tal:define="global name string:Bond, James Bond" ></span>
Now, who did you say you were again?
<b tal:content="string:$name">name here again.</b>
As you've probably guessed by now, the "global" keyword allows you to
define a particular variable as global, rather than local; this
immediately makes it available to all the elements within a template,
and eliminates the hassles of creating and maintaining complex
parent-child relationships between the elements of your user interface.
This difference between global and local variables should be clearly
understood; it's one of the most common errors newbies make, and it can
lead to lots of head-scratching when things don't behave as you expect.
Here's one more example, one which should help clear any lingering
doubts:
<span tal:define="global flavour string:strawberry"></span> <br>
-- global scope -- Current flavour is <span
tal:content="string:$flavour">flavour here</span> <br> <span
tal:define="local flavour string:peach">-- local scope -- Current
flavour is <span tal:content="string:$flavour">flavour
here</span>.</span> <br>
-- global scope -- Current flavour is <span
tal:content="string:$flavour">flavour here</span> <br>
Here's the output:
-- global scope -- Current flavour is strawberry
-- local scope -- Current flavour is peach.
-- global scope -- Current flavour is strawberry
In this case, the value of the variable "flavour" is set to
"strawberry", globally. However, in the middle section of the template,
this global value is overridden by a new local definition for the same
variable, which sets it to "peach". And once the local scope is exited,
the global value comes back and is used in all subsequent variable
accesses.