SunQuest
 
       Python
  Home arrow Python arrow Page 4 - Templating with Cheetah
Dev Shed Forums 
Administration  
AJAX  
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 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
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? 
PYTHON

Templating with Cheetah
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 15
    2005-12-27

    Table of Contents:
  • Templating with Cheetah
  • Basic Templating
  • Compiling Templates
  • More Logic

  • 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Templating with Cheetah - More Logic


    (Page 4 of 4 )

    Functions can be declared in Cheetah templates, too, using the #def directive:

    #def number($num)
    Number: $num
    #end def
    #set $x = 1
    #while $x <= 5
    $number($x)
    #set $x = $x + 1
    #end while

    Cheetah also contains #return and #pass directives for use in functions that behave just like their Python equivalents.

    It's possible to import Python modules in a template using the #import directive:

    #import time
    $time.strftime('%m/%d/%Y')

    A #from directive also exists that is identical to Python's from.

    You can also import other compiled templates, and it's possible to subclass both compiled templates and Python modules as well. Here, we define parent.tmpl:

    One: $one
    Two: $two
    Three: $three

    Compile it, and then we are able to subclass it and define methods for $one, $two and $three:

    #from parent import parent
    #extends parent
    #def one
    1
    #end def
    #def two
    2
    #end def
    #def three
    3
    #end def

    Of course, just because you can play around with logic in Cheetah templates does not mean that you should. Combining presentation and logic wherever possible defeats the purpose of templating, which is to separate the two elements of dynamic content. Too many directives within a template make the template difficult to read, anyway. In my experience, a good rule of thumb is to use template logic where actual layout elements will vary as a result. Otherwise, if it's just a simple message or something that will vary, Python does the job a lot better. More complex layout elements are probably best left to Python, too.

    Conclusion

    Cheetah offers templating that can be used with just about any Python framework. I should also note that it doesn't have to be used with web pages. It can also be used for XML documents, e-mails and just about any other format. Beyond basic templating, it also allows for logic to be placed within templates, such as conditional statements, loops and functions. It's an easy-to-use tool that's worth taking into consideration for projects that require dynamic content.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Hey, all,I know that a lot of people have heard of Cheetah, but I'm not sure all...
       · I believe it should be 'python setup.py install'.
       · file ( 'count.txt', 'r' ).write ( str ( count ) )should be file ( 'count.txt',...
       · [b]print quotation ( searchList = [{ 'quotation': quotation [0],'author':...
       · This article was nice to show the simplcity of Cheetah, but falls far short of being...
     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...





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