1
"TurboGears support for Zope Page Templates"
8
#from zope.pagetemplate.pagetemplatefile import PageTemplateFile
9
from simpletal import simpleTAL, simpleTALES
11
log = logging.getLogger("turbogears.zptsupport")
16
tinstance = _zpt_cache.get(tfile)
18
tinstance = _zpt_cache[tfile] = TemplateWrapper(simpleTAL.compileXMLTemplate(open(tfile)))
21
class TemplateWrapper(object):
23
def __init__(self, template):
24
self.template = template
26
def __call__(self, **kw):
27
context = simpleTALES.Context(allowPythonPath=1)
28
for k, v in kw.iteritems():
29
context.addGlobal(k, v)
30
s = StringIO.StringIO()
31
self.template.expand(context, s)
36
return self.template.macros
38
## class TGPageTemplateFile(PageTemplateFile):
40
## def pt_getContext(self, args=(), options={}, **ignored):
41
## namespace = super(TGPageTemplateFile, self).pt_getContext(
42
## args, options, **ignored)
43
## namespace.update(options)
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))
70
def render(self, info, format="html", fragment=False, template=None):
71
"""Renders data in the desired format.
73
@param info: the data / context itself
76
@type format: "string"
77
@para template: name of the template to use
78
@type template: string
80
tinstance = self.load_template(template)
81
#log.debug("Applying template %s" % (tinstance.filename))
83
if self.get_extra_vars:
84
data.update(self.get_extra_vars())
86
return tinstance(**data)