Deelan,
Date: Mon, 21 Jul 2003 13:18:31 +0200 From: deelan <[EMAIL PROTECTED]> To: [EMAIL PROTECTED] CC: Aaron Held <[EMAIL PROTECTED]> Subject: Re: [Webware-discuss] How-to include static HTML + TaskKit
i known cheetah but i was resistant to use it, but now i'm starting
to see the benefits. in the current cheetah-less implementation i filled my base servlet with:
def writeHeader(self): self.writeln('<html>...') # lots of HTML code
writeFooter(): self.writeln('<div>...') # lots of HTML code
but i wonder that i can use a cheetah TMPL file to store the markup for layout of the site.
recently in the cheetah ML i saw this interesting example:
from WebKit.Page import Page
class baseservlet(Page):
"""This class adds links Webware servlets to Cheetah templates.
It includes the logic to generate its HTML content by finding a Cheetah template
with the same name as this class plus the '_tmpl.tmpl' suffix.
(This template has been previously complied into a python module 'named servletname_tmpl')
It imports that template, registers itself as the template's servlet
(containing the business logic the template may need), and then renders
the template by calling its respond method and passing the resulting
HTML string to the writeln() method."""
def writeHTML(self): #print ">> baseservlet.writeHTML()" tmpl = self.template() tmpl.servlet = self self.writeln(tmpl.respond())
def template(self):
templateName = self.__class__.__name__ + '_tmpl'
importLine = 'from %s import %s as template' % (templateName, templateName)
exec(importLine)
return template()
def hasUser(self): return self.session().hasValue('user')
i'm trying to guess here, but if i understand correctly basically here an external TMPL is imported dynamically in the servlet using its name to lookup the right template fiel. a "baseservlet" subclass implements per-page servlet logic and set itself as ref servlet. i'm wondering if there's a more elegamnt way to dynamically import a class, that exec(importLine) seems a bit brutal to me
i stil have to try this, but seems a smart approach.
later,
deelan
Yes, an external template is imported dynamically. The whole exec(importLine) thing is just so I don't have to create tons of little files that exist only to define their template and have no additional business logic. As for how brutal it is, I'm not aware of anyway way to do an 'import' keyword followed by anything dynamic. Since python is interpretted anyway, the exec() should seem less brutal than comparable code in other languages. The only other idea I had was to override __import__ in the __init__.py module, and there I think you might be able to do something less brutal by hooking into the import process, but I'm not enough of a pythonista yet to investigate that one.
I've only been using this approach for a brief period, and I like it so far. I like that fact that I can change the way a page looks by changing the template it inherits from without messing with the business logic. The pure inheritence approach to combining Cheetah and Webware was too confusing for me. Big hunks of HTML inside a servlet just won't fly for me, and I hate trying to do anything non-presentation with Cheetah syntax, so this approach keeps me pretty happy. If I find anything particularly nice about this approach, or any major flaws, I'll post them to the Cheetah list.
Pete
------------------------------------------------------- This SF.net email is sponsored by: VM Ware With VMware you can run multiple operating systems on a single machine. WITHOUT REBOOTING! Mix Linux / Windows / Novell virtual machines at the same time. Free trial click here: http://www.vmware.com/wl/offer/345/0 _______________________________________________ Webware-discuss mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/webware-discuss
