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

 
Developer Updates  
Free Website Content 
 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: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    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.

     
     
    >>> 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-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT