Placement of "self" in searchList during #include

View: New views
1 Messages — Rating Filter:   Alert me  

Placement of "self" in searchList during #include

by Ben Hoyt-2 :: Rate this Message:

| View Threaded | Show Only this Message

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
calling template.

This code for this is:
https://github.com/tavisrudd/cheetah/blob/master/cheetah/Template.py#L1478

As an example of when this is an issue, consider these templates:

main.tmpl
-----
#def greet(name)
Hello, $name.
#end def
$greet('Ben')
#include 'included.tmpl'
-----

included.tmpl
-----
#def greet(name)
Bye bye, $name!
#end def
$greet('Ben')
-----

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
function.

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.append(self)

with:

            self._CHEETAH__searchList = []
            selfAdded = False
            for item in _preBuiltSearchList:
                if not selfAdded and isinstance(item, Template):
                    self._CHEETAH__searchList.append(self)
                    selfAdded = True
                self._CHEETAH__searchList.append(item)

This will place the #include template's "self" above the caller's, as
(at least we) expected.

Thanks,
Ben.

------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure
contains a definitive record of customers, application performance,
security threats, fraudulent activity, and more. Splunk takes this
data and makes sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-novd2d
_______________________________________________
Cheetahtemplate-discuss mailing list
Cheetahtemplate-discuss@...
https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss