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  
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? 
Google.com  
PYTHON

Templating with Cheetah
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 20
    2005-12-27


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

  • 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


    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.



     
     
    >>> More Python Articles          >>> More By Peyton McCullough
     

       

    PYTHON ARTICLES

    - Tuples and Other Python Object Types
    - The Dictionary Python Object Type
    - String and List Python Object Types
    - Introducing Python Object Types
    - Mobile Programming using PyS60: Advanced UI ...
    - Nested Functions in Python
    - Python Parameters, Functions and Arguments
    - Python Statements and Functions
    - Statements and Iterators in Python
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek