Zope
  Home arrow Zope arrow Page 4 - ZPT Basics (part 2)
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Developerworks
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ZOPE

ZPT Basics (part 2)
By: Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 13
    2002-09-25

    Table of Contents:
  • ZPT Basics (part 2)
  • A Little Male Bonding
  • The Number Game
  • A Question Of Scope
  • Switching Things Around
  • Comparing Apples and Oranges
  • If It's Thursday, It Must be Italy
  • Submitting To The King
  • Looking Ahead

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    TestComplete™ automates software testing for a fraction of what the big guys charge. Easy functional and load testing for all Windows, .NET, Java and Web apps. Download a free trial now.

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


     

       

    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




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway