Compiling a template converts it into Python code, which eliminates the need for Cheetah to go through a template and replace everything. This means that Cheetah can work faster. Compiling a template is very simple, and all it involves is a call to Cheetah. Take a look at this template, quote.tmpl, which provides a placeholder for a random quotation: <center> To compile this, we pass a few arguments to cheetah: cheetah compile quote.tmpl Cheetah then generates the file quote.py, which contains the class quote. The class can be imported into a Python script and then used similarly to the Template class: import random # Define a list of quotations # Pick a random quotation # Print the product Adding Logic Instead of having a Python script control all the logic for a certain template, it's possible to slip a bit of simple logic into a template. Cheetah makes this possible through special directives. For example, say that you want to display a certain message if one condition is met and a certain message if another condition is met. The #if directive can be used for this task, rather than doing work in Python. Take a look at greeting.tmpl, which displays different greetings for different times of day: #if $hour < 12 All we need to do is pass the hour of the day to the script, and it will display the appropriate message: import time hour = time.localtime() [3] The #unless directive is similar to the #if directive, but it returns the opposite of what the expression returns. If the expression is true, then the contained code is not executed. If the expression is false, then the contained code executes. Take this template for example: #unless $a Unless $a returns a true value, “< Message >” is displayed to the user. If $a does return a true value, then the message is never displayed. Cheetah also allows for loops to be used within templates. The #for directive acts as a for loop, as in listPresidents.tmpl: Presidents: To see the template in action, we need to pass a list to take the place of the $names variable: from Cheetah.Template import Template presidents = [ 'George Washington', 'John Adams', 'Thomas print Template ( file = 'listPresidents.tmpl', searchList = The #repeat directive simply repeats something a specified amount of times. For example, say I wanted to make a primitive horizontal bar graph using “|” characters. I could use the #repeat directive to do this in graph.tmpl: #repeat $x We can now substitute $x for the length of the graph: from Cheetah.Template import Template # Set the length of the bar graph print Template ( file = 'graph.tmpl', searchList = [{ 'x': x }] ) If you run the above script, however, you will see that each “|” character is put on a new line. This can be fixed using the #slurp directive: #repeat $x Lastly, the #while directive acts as a while loop: #set $x = 1 Notice that the above template uses the #set directive. This simply sets the value of a variable. Cheetah also contains #break and #continue directives for use in loops. This template will not display “4” or anything past “5”: #set $x = 1
blog comments powered by Disqus |
|
|
|
|
|
|
|