[Welcome] [TitleIndex] [WordIndex

Here's a quick and dirty module to help when using SimpleTAL.

Here's an alternative QuixoteAndSimpleTalAdvanced

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

# Compiled template cache
_cache = {}

def loadTemplate(filename):
    Load and compile a TAL template file. The compiled template is
    also cached for next time.
    mtime = os.path.getmtime(filename)
    template = _cache.get(filename,None)
    if template and template[1] == mtime:
        return template[0]
    template = simpleTAL.compileHTMLTemplate(file(filename))
    _cache[filename] = (template,mtime)
    return template

def render(filename, d=None):
    Render the template file using information in the d dictionary.
    context = simpleTALES.Context()
    if d:
        for name, value in d.items():
            context.addGlobal(name, value)
    template = loadTemplate(filename)
    out = StringIO()
    return out.getvalue()


2010-09-22 22:14