Loading ...
Sorry, an error occurred while loading the content.

[Cheetahtemplate-discuss] Placement of "self" in searchList during #include

Expand Messages
  • Ben Hoyt
    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
    Message 1 of 1 , Nov 30, 2011
      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:

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

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

      #def greet(name)
      Bye bye, $name!
      #end def

      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.


      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.
      Cheetahtemplate-discuss mailing list
    Your message has been successfully submitted and would be delivered to recipients shortly.