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