Home arrow Zope arrow Page 4 - ZPT Basics (part 2)

A Question Of Scope - Zope

Dig deeper into the world of ZPT with examples andillustrations of how to create, manipulate and use variables andconditional tests in your templates.

TABLE OF CONTENTS:
  1. ZPT Basics (part 2)
  2. A Little Male Bonding
  3. The Number Game
  4. A Question Of Scope
  5. Switching Things Around
  6. Comparing Apples and Oranges
  7. If It's Thursday, It Must be Italy
  8. Submitting To The King
  9. Looking Ahead
By: Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 13
September 25, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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.

 
 
>>> More Zope Articles          >>> More By Harish Kamath, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

ZOPE ARTICLES

- Creating Zope Products
- Plone Content Types With Archetypes
- Flat User Management in Zope
- Creating Basic Zope Applications
- Getting started with Zope for Linux and Sola...
- ZPT Basics (part 4)
- ZPT Basics (part 3)
- ZPT Basics (part 2)
- ZPT Basics (part 1)
- Exception Handling In DTML
- DTML Basics (part 4)
- DTML Basics (part 3)
- DTML Basics (part 2)
- DTML Basics (part 1)
- Using Zope With Apache

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: