If you use Cheetah, please support the project and register by clicking here:


13.6 Optimizing templates

Here are some things you can do to make your templates fill faster and user fewer CPU cycles. Before you put a lot of energy into this, however, make sure you really need to. In many situations, templates appear to initialize and fill instantaneously, so no optimization is necessary. If you do find a situation where your templates are filling slowly or taking too much memory or too many CPU cycles, we'd like to hear about it on the mailing list.

Cache $placeholders whose values don't change frequently. (Section 7.4).

Use #set for values that are very frequently used, especially if they come out of an expensive operation like a deeply.nested.structure or a database lookup. #set variables are set to Python local variables, which have a faster lookup time than Python globals or values from Cheetah's searchList.

Moving variable lookups into Python code may provide a speedup in certain circumstances. If you're just reading self attributes, there's no reason to use NameMapper lookup ($placeholders) for them. NameMapper does a lot more work than simply looking up a self attribute.

On the other hand, if you don't know exactly where the value will come from (maybe from self, maybe from the searchList, maybe from a CGI input variable, etc), it's easier to just make that an argument to your method, and then the template can handle all the NameMapper lookups for you:

#silent $myMethod($arg1, $arg2, $arg3)
Otherwise you'd have to call self.getVar('arg1') etc in your method, which is more wordy, and tedious.

If you use Cheetah, please support the project and register by clicking here: