The web application I’m working on uses Cheetah – “The Python Powered Template Engine” for web page generation and while the updated documentation is a great improvement over the old docs there remain some gaps that weren’t very clear to me when I first got started…
Of course, Cheetah’s got a lot of features so I’m certain there are things I’m missing which is part of the point of this post to start a conversation about how I’m using Cheetah and elicit feedback and exchange ideas/best practices.
The first piece I’ll toss out is simply a routine to call the Cheetah compiler on a list of templates. Running this routine alone on the CI server has caught a bunch of syntax errors that wouldn’t have been caught using runtime compiled templates.
def compileTemplates(templateFiles, outpath="ui/compiled/"): ''' Compiles templateFiles if they're out of date ''' if templateFiles == : raise Exception("No template files specified") for t in templateFiles: outfile = os.path.split(t) outfile = os.path.splitext(outfile) + ".py" if not os.path.exists(outpath + outfile) or os.path.exists(outpath + outfile) and os.path.getmtime(outpath + outfile) < os.path.getmtime(config.templatePath(t)): t = config.templatePath(t) print "Template: " + t source = Cheetah.Compiler.Compiler(file=t, moduleName=os.path.splitext(outfile)).moduleDef() f = open(outpath + outfile, "w+t") f.write(source) f.close()
The routine checks to see if the template file (.tmpl) has been modified since the last compilation and optionally recompiles.
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.