Skip to content

Instantly share code, notes, and snippets.

@jkeyoth
Created March 5, 2011 05:51
Show Gist options
  • Save jkeyoth/856161 to your computer and use it in GitHub Desktop.
Save jkeyoth/856161 to your computer and use it in GitHub Desktop.
Using templates in python with cheetah
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