jose wrote:

Hi all, I'm fishing for advice.  I have recently been looking at
different templating solutions to use with webware.  At the moment I am
looking at cheetah and zpt.  To compare them more directly I was
basically building the same project with both of them to see how they
worked.  I was using cheetah like this:

================
from WebKit.Page import Page
from Cheetah.Template import Template

Class testPage(Page):
        def writeContent(self):
                t = Template(file='template.tmpl', searchList=[self])
                self.write(t)

I know that I am taking a performance hit by using the templates in this
way, but aside from that is there any real disadvantages? Or is there a
better way to use cheetah within a servlet.

I think this is quite viable way to do.
Myself, I've built rather elaborate framework on top of Cheetah but, of course, not everyone need one. The good thing about Cheetah is that it's API is very nice and you could flex it any way you like. The main drawback - if you have rather big/complex template files debugging them could be rather difficult because error reporting is poor.


As for performance, here is a helper class I've used for caching.
The trick here is to cache classes, not instances.

class CheetahTemplateFileCache:
    """
    Caches Cheetah's template files.

    Note, that classes instead of instances are stored in a cache. This is
    required because instances are modified by Cheetah and are not safe for
    re-use and thus must be created fresh on each usage.
    """

    srcFileTag = re.compile('Source file: [^\n]+')

    def __init__(self):
        self.cache = {}
        self.lock = threading.Lock()
    def genTemplate(self, fileName):
        t = Template(file=fileName)
        temp = {}
        src = t.generatedModuleCode()
        src = self.srcFileTag.sub('XXX', src) # workaround Cheetah bug
        #open('x', 'w').write(src)
        exec src in temp
        tklass = temp['GenTemplate']
        if not tklass:
            raise Error("Can't construct template for %s" % fileName)
        return tklass
    def addToCache(self, fileName):
        tklass = self.genTemplate(fileName)
        self.cache[fileName] = (tklass, time.time())
        return tklass
    def get(self, fileName):
        "Returns a Cheetah's Template for given file name."
        self.lock.acquire()
        try:
            tklass = None
            if not self.cache.has_key(fileName):
                tklass = self.addToCache(fileName)
            else:
                tklass, mtime = self.cache[fileName]
                newmtime = os.path.getmtime(fileName)
                if newmtime > mtime:
                    tklass = self.addToCache(fileName)
            return tklass()
        finally:
            self.lock.release()

templatesCache = CheetahTemplateFileCache()


BTW just for reference, I generally like to have my servlets call and
use templates, so a single servlet can technically use different
templates depending on the circumstance that it is used.

I impl. a WebKit handler for template files, so that you could request a template directly and WebKit build a Template instance, feed data to it, render it and pass result back to browser.




-------------------------------------------------------
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
_______________________________________________
Webware-discuss mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/webware-discuss

Reply via email to