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 # Update the count print Template ( file = 'counter.tmpl', searchList = 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 /> 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: johnDoe = Profile ( 'John Doe', 'jdoe@google.com', '(555) 555- Cheetah then substitutes the appropriate attributes.
blog comments powered by Disqus |