Python
  Home arrow Python arrow Page 2 - 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 
Moblin 
JMSL Numerical Library 
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


    Templating with Cheetah - Basic Templating


    (Page 2 of 4 )

    Let's start off with a basic template with which to test out Cheetah. A simple counter will work fine. Create a file named counter.tmpl:

    Hello, This page has been viewed $counter times.

    In the above example, we will obviously want to substitute the $counter variable for the number of times the particular page has been viewed. It's pretty easy to turn our template into a real, working page in just a few lines of Python. We'll use a text file to store the count for simplicity's sake:

    import os.path
    from Cheetah.Template import Template

    # Update the count
    if os.path.exists ( 'count.txt' ):
       count = int ( file ( 'count.txt' ).read() )
       count = count + 1
    else:
       count = 1
    file ( 'count.txt', 'r' ).write ( str ( count ) )

    print Template ( file = 'counter.tmpl', searchList =
    [{ 'counter': str ( count ) }] )

    After updating the count, we simply pass the file we wish to parse and a dictionary containing the replacement value. Cheetah then does the rest and returns the result, which is printed.

    It's also possible to pass objects to Cheetah. Take a look at this template, contact.tmpl, which display's a particular person's contact information:

    <b>Name:</b> $profile.name<br />
    <b>E-Mail:</b> $profile.email<br />
    <b>Phone:</b> $profile.phone<br />
    <b>MSN:</b> $profile.msn<br />
    <b>AIM:</b> $profile.aim<br />
    <b>ICQ:</b> $profile.icq<br />
    <b>YIM:</b> $profile.yim

    All we need to do is create an object with the above properties and pass it under the profile key:

    from Cheetah.Template import Template

    class Profile:
       def __init__ ( self, name, email, phone, msn, aim, icq, yim ):
          self.name = name
          self.email = email
          self.phone = phone
          self.msn = msn
          self.aim = aim
          self.icq = icq
          self.yim = yim

    johnDoe = Profile ( 'John Doe', 'jdoe@google.com', '(555) 555-
    5555', 'jdoe@google.com', 'JDoe', '0123456789', 'JDoe' )
    print Template ( file = 'contact.tmpl', searchList =
    [{ 'profile': johnDoe }] )

    Cheetah then substitutes the appropriate attributes.

    More Python Articles
    More By Peyton McCullough


       · 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