RE: [Cheetahtemplate-discuss] Subclassing "Template" and using respond()
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
# do some preliminary stuff
output = self.respond()
# do some wrapup stuff
tmplt = MyTemplate(file='somefile')
From: Mike Orr [mailto:mso@...]
Sent: Friday, July 09, 2004 12:54 AM
To: Donnie Hale
Subject: Re: [Cheetahtemplate-discuss] Subclassing "Template" and using
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
> 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
> want to use a template file as the base class - I want to just use a
> 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 -
> 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 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')
<__main__.MyTemplate instance at 0x4022a08c>
>>> print tmplt
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