diff options
| author | Chris McDonough <chrism@agendaless.com> | 2008-07-19 01:17:21 +0000 |
|---|---|---|
| committer | Chris McDonough <chrism@agendaless.com> | 2008-07-19 01:17:21 +0000 |
| commit | 4df5751de28947538da491dc8ebe0dfb27f742d5 (patch) | |
| tree | 491c7029cb50f4baed54ddf7485b4aa0bb02d739 /repoze/bfg/template.py | |
| parent | 7bce0d6d925370dd5721100c28693f806b68344b (diff) | |
| download | pyramid-4df5751de28947538da491dc8ebe0dfb27f742d5.tar.gz pyramid-4df5751de28947538da491dc8ebe0dfb27f742d5.tar.bz2 pyramid-4df5751de28947538da491dc8ebe0dfb27f742d5.zip | |
- The concept of "view factories" was removed in favor of always
calling a view, which is a callable that returns a response
directly (as opposed to returning a view). As a result, the
``factory`` attribute in the bfg:view ZCML statement has been
renamed to ``view``. Various interface names were changed also.
- ``render_template`` and ``render_transform`` no longer return a
Response object. Instead, these return strings. The old behavior
can be obtained by using ``render_template_to_response`` and
``render_transform_to_response``.
Diffstat (limited to 'repoze/bfg/template.py')
| -rw-r--r-- | repoze/bfg/template.py | 79 |
1 files changed, 46 insertions, 33 deletions
diff --git a/repoze/bfg/template.py b/repoze/bfg/template.py index 8ade2fb74..0d390fecf 100644 --- a/repoze/bfg/template.py +++ b/repoze/bfg/template.py @@ -1,6 +1,8 @@ import os import sys +from webob import Response + from zope.component import queryUtility from zope.component.interfaces import ComponentLookupError from zope.component import getSiteManager @@ -8,28 +10,25 @@ from zope.component import getSiteManager from zope.interface import classProvides from zope.interface import implements -from webob import Response - -from repoze.bfg.interfaces import IView -from repoze.bfg.interfaces import INodeView from repoze.bfg.interfaces import ITemplateFactory +from repoze.bfg.interfaces import ITemplate +from repoze.bfg.interfaces import INodeTemplate class Z3CPTTemplateFactory(object): classProvides(ITemplateFactory) - implements(IView) + implements(ITemplate) def __init__(self, path): from z3c.pt import PageTemplateFile self.template = PageTemplateFile(path) - def __call__(self, *arg, **kw): + def __call__(self, **kw): result = self.template.render(**kw) - response = Response(result) - return response + return result class XSLTemplateFactory(object): classProvides(ITemplateFactory) - implements(INodeView) + implements(INodeTemplate) def __init__(self, path): self.path = path @@ -37,8 +36,7 @@ class XSLTemplateFactory(object): def __call__(self, node, **kw): processor = get_processor(self.path) result = str(processor(node, **kw)) - response = Response(result) - return response + return result # Manage XSLT processors on a per-thread basis import threading @@ -61,55 +59,70 @@ def get_processor(xslt_fn): def package_path(package): return os.path.abspath(os.path.dirname(package.__file__)) -def registerTemplate(template, path): +def registerTemplate(type, template, path): try: sm = getSiteManager() except ComponentLookupError: pass else: - sm.registerUtility(template, IView, name=path) + sm.registerUtility(template, type, name=path) def render_template(path, **kw): """ Render a z3c.pt (ZPT) template at the package-relative path (may also be absolute) using the kwargs in ``*kw`` as top-level - names and return a Response object. """ + names and return a string. """ # XXX use pkg_resources + path = caller_path(path) - if not os.path.isabs(path): - package_globals = sys._getframe(1).f_globals - package_name = package_globals['__name__'] - package = sys.modules[package_name] - prefix = package_path(package) - path = os.path.join(prefix, path) - - template = queryUtility(IView, path) + template = queryUtility(ITemplate, path) if template is None: if not os.path.exists(path): raise ValueError('Missing template file: %s' % path) template = Z3CPTTemplateFactory(path) - registerTemplate(template, path) + registerTemplate(ITemplate, template, path) return template(**kw) +def render_template_to_response(path, **kw): + """ Render a z3c.pt (ZPT) template at the package-relative path + (may also be absolute) using the kwargs in ``*kw`` as top-level + names and return a Response object. """ + path = caller_path(path) + result = render_template(path, **kw) + return Response(result) + def render_transform(path, node, **kw): """ Render a XSL template at the package-relative path (may also be absolute) using the kwargs in ``*kw`` as top-level names and - return a Response object.""" + return a string.""" # Render using XSLT + path = caller_path(path) - if not os.path.isabs(path): - package_globals = sys._getframe(1).f_globals - package_name = package_globals['__name__'] - package = sys.modules[package_name] - prefix = package_path(package) - path = os.path.join(prefix, path) - - template = queryUtility(IView, path) + template = queryUtility(INodeTemplate, path) if template is None: if not os.path.exists(path): raise ValueError('Missing template file: %s' % path) template = XSLTemplateFactory(path) - registerTemplate(template, path) + registerTemplate(INodeTemplate, template, path) return template(node, **kw) + +def render_transform_to_response(path, node, **kw): + """ Render a XSL template at the package-relative path (may also + be absolute) using the kwargs in ``*kw`` as top-level names and + return a Response object.""" + path = caller_path(path) + result = render_transform(path, node, **kw) + return Response(result) + +def caller_path(path): + if not os.path.isabs(path): + package_globals = sys._getframe(2).f_globals + package_name = package_globals['__name__'] + package = sys.modules[package_name] + prefix = package_path(package) + path = os.path.join(prefix, path) + return path + + |
