If you look closely at all the examples on the previous page, you willsee 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 theluxury of using a variable only at the time of definition in real-worlddevelopment.
So the question you should be asking is, can a variable defined in oneparticular 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 HTMLelement, but used it in another. And when I attempt to have Zope renderthis page for me, it barfs all over the screen...
Does this mean that variables can only be accessed within the sameelement? I thought so...until a quick read of the TAL documentationclarified 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 ofthat element. In the example above, the <b> element is not a child ofthe <span> element containing the variable definition - which is whyZope can't find it.
Now, if I were to rewrite the code above to make the <b> element a childof 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 errorscreen thrown at you, it's not really an optimal solution. Which is whyTAL 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 todefine a particular variable as global, rather than local; thisimmediately makes it available to all the elements within a template,and eliminates the hassles of creating and maintaining complexparent-child relationships between the elements of your user interface.
This difference between global and local variables should be clearlyunderstood; it's one of the most common errors newbies make, and it canlead to lots of head-scratching when things don't behave as you expect.Here's one more example, one which should help clear any lingeringdoubts:
<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 samevariable, which sets it to "peach". And once the local scope is exited,the global value comes back and is used in all subsequent variableaccesses.