From: Ben Hoyt <benhoyt@gm...> - 2011-11-30 22:48:15
Hi Cheetah folks,
For some reason when you #include a file, Cheetah adds the new
Template instance's "self" to the end of the searchList, instead of at
the beginning (or at least above the calling template's self). This
causes rather non-intuitive behaviour when you're #def-ining a
function in an included template that's already been #def-ined in a
This code for this is:
As an example of when this is an issue, consider these templates:
Bye bye, $name!
You'd expect this to print out "Hello, Ben.\n\nBye bye, Ben!" but
instead it prints out "Hello, Ben.\n\nHello, Ben.", because
main.tmpl's "self" is above included.tmpl's "self" in the searchList,
so the $greet() in included.tmpl is still calling main.tmpl's greet
You can work around this by explicitly calling $self.greet() in the
included template, but it's rare to use "self." in Cheetah templates
and so is easy to forget, leading to potential bugs.
So my question is, why doesn't #include add the included template's
"self" to the searchList before the calling template's "self", to give
the more natural search order? I'm tempted to change this behaviour,
but there's probably a good reason for it, so I'm hesitant.
To change it, you can replace these two lines in Template.py:
self._CHEETAH__searchList = list(_preBuiltSearchList)
self._CHEETAH__searchList = 
selfAdded = False
for item in _preBuiltSearchList:
if not selfAdded and isinstance(item, Template):
selfAdded = True
This will place the #include template's "self" above the caller's, as
(at least we) expected.