1
"TurboGears support for Zope Page Templates"
7
#from zope.pagetemplate.pagetemplatefile import PageTemplateFile
8
from simpletal import simpleTAL
10
log = logging.getLogger("turbogears.zptsupport")
15
tinstance = _zpt_cache.get(tfile)
17
tinstance = _zpt_cache[tfile] = simpleTAL.compileXMLTemplate(open(tfile))
20
## class TGPageTemplateFile(PageTemplateFile):
22
## def pt_getContext(self, args=(), options={}, **ignored):
23
## namespace = super(TGPageTemplateFile, self).pt_getContext(
24
## args, options, **ignored)
25
## namespace.update(options)
28
class TurboZpt(object):
31
def __init__(self, extra_vars_func=None):
32
self.get_extra_vars = extra_vars_func
34
def load_template(self, classname, loadingSite=False):
35
"""Searches for a template along the Python path.
37
Template files must end in ".pt" and be in legitimate packages.
38
Templates are automatically checked for changes and reloaded as
41
divider = classname.rfind(".")
43
package = classname[0:divider]
44
basename = classname[divider+1:]
46
raise ValueError, "All templates must be in a package"
48
tfile = pkg_resources.resource_filename(
49
package, "%s.%s" % (basename, self.extension))
52
def render(self, info, format="html", fragment=False, template=None):
53
"""Renders data in the desired format.
55
@param info: the data / context itself
58
@type format: "string"
59
@para template: name of the template to use
60
@type template: string
62
tinstance = self.load_template(template)
63
#log.debug("Applying template %s" % (tinstance.filename))
65
if self.get_extra_vars:
66
data.update(self.get_extra_vars())
68
from simpletal import simpleTALES
69
context = simpleTALES.Context(allowPythonPath=1)
70
for k, v in info.iteritems():
71
context.addGlobal(k, v)
73
s = StringIO.StringIO()
74
tinstance.expand(context, s)