Created
March 5, 2011 05:51
-
-
Save jkeyoth/856161 to your computer and use it in GitHub Desktop.
Using templates in python with cheetah
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from Cheetah.Template import Template | |
#The template text. Probably read in from a file for this project, but for an example I'll just use a string | |
templateDefinition = "This is a template. $exclamation$asdf" | |
#set up Template object | |
template = Template(templateDefinition) | |
#set the exclamation variable. | |
template.exclamation = "Woohoo" | |
#you can name the variables anything | |
template.asdf = "!!!!111!!!oneoneone!!" | |
#get the filled in string | |
filled = str(template) #filled = "This is a template. Woohoo!!!!111!!!oneoneone!!" | |
#or print the filled in string | |
print filled # prints: "This is a template. Woohoo!!!!111!!!oneoneone!!" | |
#you can also use a dict to pass the values to use in the template: | |
names = {"exclamation" : "Yay", "asdf" : ":)"} | |
template2 = Template(templateDefinition, names) | |
print template2 #prints: "This is a template. Yay:)" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment