durusmail: quixote-users: Using simpletal (zpt) with quixote.
Using simpletal (zpt) with quixote.
2003-12-11
2003-12-12
2003-12-12
2003-12-12
Using simpletal (zpt) with quixote.[ca]
2003-12-12
2003-12-12
Using simpletal (zpt) with quixote.
Vineet Jain
2003-12-12
Thanks that worked. I use the following code to preload my cache of simple
tal templates:

from path import path
from Singleton import Singleton
from ApplicationConfig import ApplicationConfig
import logging
from simpletal import simpleTAL, simpleTALES


class SimpleTALTemplateCache(Singleton):

        _templateCache = {}

        def init(self):
                templateconfig = ApplicationConfig().config.templateconfig
                logging.info('Initializing template cache')

                if (templateconfig.preload ):
                        dir = path(templateconfig.startsearchinrootfolder)
                        for d in
dir.walkdirs(templateconfig.templatefoldername):
                                td = path(d)
                                for f in
td.walkfiles('*.'+templateconfig.templateextension):
                                        self._templateCache[f.name] =
SimpleTALFile(f.abspath())
                logging.debug(self._templateCache)

        def getTemplate(self, templateName):
                logging.debug('template cahce is :')
                logging.debug(self._templateCache)
                templateconfig = ApplicationConfig().config.templateconfig
                templateName = templateName+'.'+templateconfig.templateextension
                if (templateconfig.checkfornewversion):
                        logging.debug('checking if new version of template:
'+templateName+'
exists')
                        templateFileOld = self._templateCache[templateName]
                        f = path(templateFileOld._sourcePath)
                        if (f.mtime != templateFileOld._lastModifiedTime):
                                logging.debug('new version found for
'+templateName+'; loading file from
the following path'+f.abspath())
                                self._templateCache[templateName] =
SimpleTALFile(f.abspath())
                        else:
                                logging.debug('New version of template:
'+templateName+' not found.
Returning cached template')
                else:
                        logging.debug('New version of template: '+templateName+'
not found.
Returning cached template')
                return self._templateCache[templateName]._template


class SimpleTALFile:

        def __init__(self, filepath):

                templatefile = path(filepath)
                self._template =
simpleTAL.compileHTMLTemplate(templatefile.text())
                self._lastModifiedTime = templatefile.mtime
                self._sourcePath = filepath



















-----Original Message-----
From: quixote-users-bounces+vineet=eswap.com@mems-exchange.org
[mailto:quixote-users-bounces+vineet=eswap.com@mems-exchange.org]On
Behalf Of Matt Goodall
Sent: Thursday, December 11, 2003 8:01 PM
To: vinjvinj@yahoo.com
Cc: quixote-users@mems-exchange.org
Subject: Re: [Quixote-users] Using simpletal (zpt) with quixote.


Vinj Vinj wrote:

>I'm setup to use quixote with twsited and would like
>to use zpt (simpletal implementation). I had the
>following line which worked with webware but fails in
>quixote:
>
>return (template.expand (context, response))
>
>I guess simpletal is setup to expect a stream(file)
>like object which the response object is not.
>
>I looked at the publish code and though I had a
>workaround, except that the some of the functions in
>publish are over written by twisted_http.
>
One answer is to write to a StringIO object, there may be others.

As it happens, I was playing with the same combination just the other
day. Below is a small class I wrote which compiles templates, caches
them and makes rendering the template trivial.

from simpletal import simpleTAL, simpleTALES
from cStringIO import StringIO
import os.path

class TemplateRenderer:

    def __init__(self):
        self.cache = {}

    def getTemplate(self, filename):
        mtime = os.path.getmtime(filename)
        template = self.cache.get(filename,None)
        if template and template[1] == mtime:
            return template[0]
        template = simpleTAL.compileHTMLTemplate(file(filename))
        self.cache[filename] = (template,mtime)
        return template

    def render(self, filename, d=None):
        context = simpleTALES.Context()
        if d:
            for name, value in d.items():
                context.addGlobal(name, value)
        template = self.getTemplate(filename)
        out = StringIO()
        template.expand(context,out)
        return out.getvalue()

# Create one
tr = TemplateRenderer()

Then your page would do something like (untested):

class Page:
    def _q_index(self, request):
        d = {'name': 'Matt', 'somethingElse': 'whatever'}
        return tr.render('template.html', d)

There may be a (much) better way of doing all this (it's pretty much the
first time I've ever used either of Quixote and SimpleTAL) but I hope it
helps.

Cheers, Matt

--
Matt Goodall, Pollenation Internet Ltd
w: http://www.pollenation.net
e: matt@pollenation.net



_______________________________________________
Quixote-users mailing list
Quixote-users@mems-exchange.org
http://mail.mems-exchange.org/mailman/listinfo/quixote-users



reply