128.6.1
by Michael Hudson
add a lightly hacked copy of turbozpt (it's not much code) |
1 |
"TurboGears support for Zope Page Templates"
|
2 |
||
128.6.40
by Michael Hudson
remove the obscure here hack and tidy up turbozpt some more |
3 |
import logging |
4 |
import os |
|
128.6.1
by Michael Hudson
add a lightly hacked copy of turbozpt (it's not much code) |
5 |
import pkg_resources |
6 |
||
128.6.56
by Michael Hudson
some kind of progress |
7 |
#from zope.pagetemplate.pagetemplatefile import PageTemplateFile
|
8 |
from simpletal import simpleTAL |
|
128.6.40
by Michael Hudson
remove the obscure here hack and tidy up turbozpt some more |
9 |
|
128.6.1
by Michael Hudson
add a lightly hacked copy of turbozpt (it's not much code) |
10 |
log = logging.getLogger("turbogears.zptsupport") |
11 |
||
128.6.40
by Michael Hudson
remove the obscure here hack and tidy up turbozpt some more |
12 |
_zpt_cache = {} |
13 |
||
14 |
def zpt(tfile): |
|
15 |
tinstance = _zpt_cache.get(tfile) |
|
16 |
if tinstance is None: |
|
128.6.56
by Michael Hudson
some kind of progress |
17 |
tinstance = _zpt_cache[tfile] = simpleTAL.compileXMLTemplate(open(tfile)) |
128.6.40
by Michael Hudson
remove the obscure here hack and tidy up turbozpt some more |
18 |
return tinstance |
19 |
||
128.6.56
by Michael Hudson
some kind of progress |
20 |
## class TGPageTemplateFile(PageTemplateFile):
|
128.6.40
by Michael Hudson
remove the obscure here hack and tidy up turbozpt some more |
21 |
|
128.6.56
by Michael Hudson
some kind of progress |
22 |
## def pt_getContext(self, args=(), options={}, **ignored):
|
23 |
## namespace = super(TGPageTemplateFile, self).pt_getContext(
|
|
24 |
## args, options, **ignored)
|
|
25 |
## namespace.update(options)
|
|
26 |
## return namespace
|
|
128.6.40
by Michael Hudson
remove the obscure here hack and tidy up turbozpt some more |
27 |
|
128.6.30
by Michael Hudson
clean up dusty parts of turbozpt |
28 |
class TurboZpt(object): |
128.6.1
by Michael Hudson
add a lightly hacked copy of turbozpt (it's not much code) |
29 |
extension = "pt" |
128.6.30
by Michael Hudson
clean up dusty parts of turbozpt |
30 |
|
31 |
def __init__(self, extra_vars_func=None): |
|
32 |
self.get_extra_vars = extra_vars_func |
|
33 |
||
128.6.1
by Michael Hudson
add a lightly hacked copy of turbozpt (it's not much code) |
34 |
def load_template(self, classname, loadingSite=False): |
35 |
"""Searches for a template along the Python path.
|
|
36 |
||
37 |
Template files must end in ".pt" and be in legitimate packages.
|
|
128.6.30
by Michael Hudson
clean up dusty parts of turbozpt |
38 |
Templates are automatically checked for changes and reloaded as
|
39 |
neccessary.
|
|
128.6.1
by Michael Hudson
add a lightly hacked copy of turbozpt (it's not much code) |
40 |
"""
|
41 |
divider = classname.rfind(".") |
|
42 |
if divider > -1: |
|
43 |
package = classname[0:divider] |
|
44 |
basename = classname[divider+1:] |
|
45 |
else: |
|
46 |
raise ValueError, "All templates must be in a package" |
|
47 |
||
128.6.30
by Michael Hudson
clean up dusty parts of turbozpt |
48 |
tfile = pkg_resources.resource_filename( |
49 |
package, "%s.%s" % (basename, self.extension)) |
|
50 |
return zpt(tfile) |
|
128.6.1
by Michael Hudson
add a lightly hacked copy of turbozpt (it's not much code) |
51 |
|
52 |
def render(self, info, format="html", fragment=False, template=None): |
|
128.6.30
by Michael Hudson
clean up dusty parts of turbozpt |
53 |
"""Renders data in the desired format.
|
54 |
||
55 |
@param info: the data / context itself
|
|
56 |
@type info: dict
|
|
57 |
@para format: "html"
|
|
58 |
@type format: "string"
|
|
59 |
@para template: name of the template to use
|
|
60 |
@type template: string
|
|
61 |
"""
|
|
62 |
tinstance = self.load_template(template) |
|
128.6.56
by Michael Hudson
some kind of progress |
63 |
#log.debug("Applying template %s" % (tinstance.filename))
|
128.6.30
by Michael Hudson
clean up dusty parts of turbozpt |
64 |
data = dict() |
65 |
if self.get_extra_vars: |
|
66 |
data.update(self.get_extra_vars()) |
|
67 |
data.update(info) |
|
128.6.56
by Michael Hudson
some kind of progress |
68 |
from simpletal import simpleTALES |
69 |
context = simpleTALES.Context(allowPythonPath=1) |
|
70 |
for k, v in info.iteritems(): |
|
71 |
context.addGlobal(k, v) |
|
72 |
import StringIO |
|
73 |
s = StringIO.StringIO() |
|
74 |
tinstance.expand(context, s) |
|
75 |
return s.getvalue() |