| Templating with Python Posted by: v0id in Python, Cheetah on Jun 02, 2008 |
There's lots of different scripting languages around nowadays aimed on web development, like PHP, Ruby, the .NETs, and so on. But it's also possible to do web development using some alternative languages, which commonly isn't known as languages for web development, like Python. And that's exactly what this blog entry is going to be about. I'll show you how to generate HTML-pages easily with Python and templates.
I'm going to use much of the code I used in my previous blog entry, "RSS and Python," so you should probably read that one first.
There is multiple template engines for Python, but I'll only use one of them here. The one I'll be using is called Cheetah, because of its nice syntax and interface in Python. Another nice engine is called Mako, which works somewhat like Cheetah does. Enough about this, let's get started.
The goal is to create a list of links using the feed from "RSS and Python." Before we start making some Python code, we shall create the template file. This is fairly easy, because it's just simple HTML, with a bit of control flow and logic (# as prefix) and variables ($ as prefix, like in PHP).
<html>
<head>
<title>$titleText</title>
</head>
<body>
#for $entry in $entryList
<a href="$entry[1]">$entry[0]</a>
#end for
</body>
</html>
The #for-statement in Cheetah has a different syntax from the Python one, but besides that is the functionality the same. We'll not have to care about the variables right now, they'll be explained later. Now let's continue, and make some Python code.
First we're going to load the template file into our Python script. It's pretty easy, as we only need a single instance, Template. We'll pass a filename to its constructor, but passing text directly to it would be just as fine as well.
from Cheetah.Template import Template
template = Template(file="test.tmpl")
Cheetah does unfortunately not have support for dictionaries, which we used in "RSS and Python," so we'll have to create a list with the data we're going to use. That is fortunately easy to do.
data = []
for item in feed["items"]:
data.append([item["title"], item["link]])
Now we're ready to generate the real HTML-page, and it's now that the Cheetah variables become important. Setting a variable is really easy. The only thing you'll have to know is the variable name. The variable will become a member of the instance we have, template, so you're easily able to give it a new value.
template.titleText = "Entries from %s" % url
template.entryList = data
The Python url variable does simply contain the URL of the feed. You can create this one yourself as a normal string.
Now to the most interesting part: get the generated content! All Cheetah instances have the special keyword __str__ overloaded, so if we specify the instance as string, we'll get the generated content in return.
print str(template)
And voilà! The generated content will be printed to the screen. If you want to see the result in a browser, you shall save the code in a Python-file, and run it, while sending sending its output to an HTML-file.
$ python test.py > test.html
Now open test.html using your favorite browser, and see the result.
I'm going to use much of the code I used in my previous blog entry, "RSS and Python," so you should probably read that one first.
There is multiple template engines for Python, but I'll only use one of them here. The one I'll be using is called Cheetah, because of its nice syntax and interface in Python. Another nice engine is called Mako, which works somewhat like Cheetah does. Enough about this, let's get started.
The goal is to create a list of links using the feed from "RSS and Python." Before we start making some Python code, we shall create the template file. This is fairly easy, because it's just simple HTML, with a bit of control flow and logic (# as prefix) and variables ($ as prefix, like in PHP).
<html>
<head>
<title>$titleText</title>
</head>
<body>
#for $entry in $entryList
<a href="$entry[1]">$entry[0]</a>
#end for
</body>
</html>
The #for-statement in Cheetah has a different syntax from the Python one, but besides that is the functionality the same. We'll not have to care about the variables right now, they'll be explained later. Now let's continue, and make some Python code.
First we're going to load the template file into our Python script. It's pretty easy, as we only need a single instance, Template. We'll pass a filename to its constructor, but passing text directly to it would be just as fine as well.
from Cheetah.Template import Template
template = Template(file="test.tmpl")
Cheetah does unfortunately not have support for dictionaries, which we used in "RSS and Python," so we'll have to create a list with the data we're going to use. That is fortunately easy to do.
data = []
for item in feed["items"]:
data.append([item["title"], item["link]])
Now we're ready to generate the real HTML-page, and it's now that the Cheetah variables become important. Setting a variable is really easy. The only thing you'll have to know is the variable name. The variable will become a member of the instance we have, template, so you're easily able to give it a new value.
template.titleText = "Entries from %s" % url
template.entryList = data
The Python url variable does simply contain the URL of the feed. You can create this one yourself as a normal string.
Now to the most interesting part: get the generated content! All Cheetah instances have the special keyword __str__ overloaded, so if we specify the instance as string, we'll get the generated content in return.
print str(template)
And voilà! The generated content will be printed to the screen. If you want to see the result in a browser, you shall save the code in a Python-file, and run it, while sending sending its output to an HTML-file.
$ python test.py > test.html
Now open test.html using your favorite browser, and see the result.