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

Re: [Cheetahtemplate-discuss] Subclassing "Template" and using respond()

Expand Messages
  • Terrel Shumway
    ... That s exactly how it works. In fact that is one of the examples in the Users Guide: ===================================== Since Cheetah is extremely
    Message 1 of 3 , Jul 8 9:07 PM
    • 0 Attachment
      Donnie Hale wrote:

      >tmplt = MyTemplate(file='somefile')
      >
      >will the generated base class of the template class be MyTemplate, or will
      >it still be Template. This would ensure that the common stuff would be
      >available to template file code that would get executed when I call
      >tmplt.respond(). If the base class will be MyTemplate, that's perfect - it's
      >what I'm looking for.
      >
      That's exactly how it works. In fact that is one of the examples in the
      Users Guide:

      =====================================

      Since Cheetah is extremely flexible, you can achieve the same result
      this way:

      >>> t2 = Template(templateDef)
      >>> t2.title = 'Hello World Example!'
      >>> t2.contents = 'Hello World'
      >>> print t2
      [ ... same output as the first example above ... ]
      >>> t2.title = 'Example #2'
      >>> t2.contents = 'Hello World!'
      >>> print t2
      [ ... same as Example #2 above ... ]

      Or this way:

      >>> class Template3(Template):
      >>> title = 'Hello World Example!'
      >>> contents = 'Hello World!'
      >>> t3 = Template3(templateDef)
      >>> print t3

      [ ... you get the picture ... ]

      The template definition can also come from a file instead of a string,
      as we will see in section 4.1
      <http://cheetahtemplate.org/docs/users_guide_html/users_guide.html#howWorks.constructing>.


      =====================================




      -------------------------------------------------------
      This SF.Net email sponsored by Black Hat Briefings & Training.
      Attend Black Hat Briefings & Training, Las Vegas July 24-29 -
      digital self defense, top technical experts, no vendor pitches,
      unmatched networking opportunities. Visit www.blackhat.com
      _______________________________________________
      Cheetahtemplate-discuss mailing list
      Cheetahtemplate-discuss@...
      https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
    • Donnie Hale
      Mike, That s a great explanation, and I appreciate it. I should have picked up on some of this from the simple examples, but I missed it. The part on
      Message 2 of 3 , Jul 9 3:41 PM
      • 0 Attachment
        Mike,

        That's a great explanation, and I appreciate it. I should have picked up on
        some of this from the simple examples, but I missed it. The part on
        .respond() not being inherited is good to know.

        On that front, will it work if I create a method in MyTemplate which calls
        respond() within it (i.e. will the grafted-in respond() method get correctly
        invoked)?

        class MyTemplate(Template):
        def wrapRespond():
        # do some preliminary stuff
        output = self.respond()
        # do some wrapup stuff
        return output

        tmplt = MyTemplate(file='somefile')
        print tmplt.wrapRespond()

        Thanks again,

        Donnie


        -----Original Message-----
        From: Mike Orr [mailto:mso@...]
        Sent: Friday, July 09, 2004 12:54 AM
        To: Donnie Hale
        Cc: cheetahtemplate-discuss@...
        Subject: Re: [Cheetahtemplate-discuss] Subclassing "Template" and using
        respond()


        On Thu, Jul 08, 2004 at 11:51:43PM -0400, Donnie Hale wrote:
        > I've not gotten a chance to try this yet, but it's next on my list; and
        the
        > docs make me wonder if this will work or not.
        >
        > I'd like to subclass Template (e.g. class MyTemplate(Template):) so I can
        > put some common stuff that my templates will use in a single place. I
        don't
        > want to use a template file as the base class - I want to just use a
        regular
        > python file - there's no template/UI-related aspect to the common stuff,
        > just code. My question is if I instantiate the template object using
        > something like:
        >
        > tmplt = MyTemplate(file='somefile')
        >
        > will the generated base class of the template class be MyTemplate, or will
        > it still be Template. This would ensure that the common stuff would be
        > available to template file code that would get executed when I call
        > tmplt.respond(). If the base class will be MyTemplate, that's perfect -
        it's
        > what I'm looking for. If not, how difficult would it be to enhance Cheetah
        > to allow for this capability?

        Assume /tmp/x.tmpl contains "$myMethod1\n".

        % python
        Python 2.3.4 (#1, Jun 2 2004, 23:35:28)
        [GCC 2.95.4 20011002 (Debian prerelease)] on linux2
        Type "help", "copyright", "credits" or "license" for more information.
        >>> from Cheetah.Template import Template
        >>> class MyTemplate(Template):
        ... def myMethod1(self):
        ... return "zzz"
        ...
        >>> tmplt = MyTemplate(file='/tmp/x.tmpl')
        >>> tmplt
        <__main__.MyTemplate instance at 0x4022a08c>
        >>> print tmplt
        zzz
        >>> tmplt.respond()
        'zzz\n'
        >>>


        So yes, the dynamically-complied class thinks it's a MyTemplate, and
        .myMethod1 is visible as expected. The Template methods (e.g., .__init__)
        also show through, otherwise the template wouldn't work at all.

        .respond isn't inherited from anywhere since it's specific to the template
        definition. It's created during .__init__ and grafed into the instance.

        --
        -Mike Orr (aka. Sluggo), mso@... (iron@...)
        http://sluggo.kicks-ass.org/ Cxu vi parolas Esperante?




        -------------------------------------------------------
        This SF.Net email sponsored by Black Hat Briefings & Training.
        Attend Black Hat Briefings & Training, Las Vegas July 24-29 -
        digital self defense, top technical experts, no vendor pitches,
        unmatched networking opportunities. Visit www.blackhat.com
        _______________________________________________
        Cheetahtemplate-discuss mailing list
        Cheetahtemplate-discuss@...
        https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
      Your message has been successfully submitted and would be delivered to recipients shortly.