Home arrow Python arrow Templating with Cheetah

Templating with Cheetah

Templating makes it easier to handle dynamic content on web pages. One of the better templating frameworks for Python is Cheetah. Keep reading to find out more.

TABLE OF CONTENTS:
  1. Templating with Cheetah
  2. Basic Templating
  3. Compiling Templates
  4. More Logic
By: Peyton McCullough
Rating: starstarstarstarstar / 21
December 27, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Introduction

The creation of dynamic web content is made much easier by using templates. These templates include substitution variables that are replaced with the proper value when the content is delivered to the user. There are a number of languages and frameworks that offer templating to developers, but Cheetah is one of Python's frontrunners. It allows templates to be made and then converted into Python objects for use within Python applications, and it works with pretty much every Python framework. For example, Cheetah could start with a template that looks like this:

$header
Hello, $name. Your last visit was on $date.
$footer

Cheetah would then compile it into a module, and $header, $name, $date and $footer could be substituted for just about anything, complete with any bells and whistles. In this article, we'll examine Cheetah and what it has to offer to Python developers looking to simplify dynamic content generation.

Installing Cheetah

Cheetah may be obtained from SourceForge:

http://sourceforge.net/project/showfiles.php?group_id=28961

Simply unarchive Cheetah and install it:

python install setup.py

Windows users will likely want to take a few extra steps when installing Cheetah. The first is to rename the file cheetah since it does not come with any extension to identify it as a Python file. It's located in the Scripts directory of your Python installation. Simply rename it cheetah.py, and if you are dying to lose the extension, create a file in the same directory called cheetah.bat:

cheetah.py %*

Next, it is recommended that you obtain a file named _namemapper.pyd from SourceForge. It speeds up Cheetah on Windows:

http://prdownloads.sourceforge.net/cheetahtemplate/_namemapper.pyd?download

Place the file in the Lib/site-packages/Cheetah directory of your Python installation.

You'll probably want to add Cheetah to your Path environmental variable, too. If you've never done anything like this before (and since this article is intended to be useful to people of various levels of experience), simply go to the Properties of My Computer, click the Advanced tab, and then click the Environmental Variables button. Then, add  a semicolon to Path followed by the path to cheetah's location. For example:

;C:\Python24\Scripts

You'll want to make sure Python is there, too.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- 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

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: