1
"TurboGears support for Zope Page Templates"
7
from simpletal import simpleTAL, simpleTALES
12
tinstance = _zpt_cache.get(tfile)
14
if tinstance is None or tinstance.stat != stat:
15
tinstance = _zpt_cache[tfile] = TemplateWrapper(
16
simpleTAL.compileXMLTemplate(open(tfile)), tfile, stat)
20
class TemplateWrapper(object):
22
def __init__(self, template, filename, stat):
23
self.template = template
24
self.filename = filename
27
def expand(self, **info):
28
context = simpleTALES.Context(allowPythonPath=1)
29
for k, v in info.iteritems():
30
context.addGlobal(k, v)
31
s = StringIO.StringIO()
32
self.template.expandInline(context, s)
35
def expand_(self, f, **info):
36
context = simpleTALES.Context(allowPythonPath=1)
37
for k, v in info.iteritems():
38
context.addGlobal(k, v)
39
self.template.expand(context, f, 'utf-8')
43
return self.template.macros
46
class TurboZpt(object):
49
def __init__(self, extra_vars_func=None):
50
self.get_extra_vars = extra_vars_func
52
def load_template(self, classname, loadingSite=False):
53
"""Searches for a template along the Python path.
55
Template files must end in ".pt" and be in legitimate packages.
56
Templates are automatically checked for changes and reloaded as
59
divider = classname.rfind(".")
61
package = classname[0:divider]
62
basename = classname[divider+1:]
64
raise ValueError, "All templates must be in a package"
66
tfile = pkg_resources.resource_filename(
67
package, "%s.%s" % (basename, self.extension))