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.
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.
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:
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: