summaryrefslogtreecommitdiff
path: root/repoze/bfg/template.py
diff options
context:
space:
mode:
authorChris McDonough <chrism@agendaless.com>2008-07-14 18:27:19 +0000
committerChris McDonough <chrism@agendaless.com>2008-07-14 18:27:19 +0000
commited334fdfefcdfdc570cfcd42aff171b177b76240 (patch)
treee716196772096510a99f8445945f97c4af78680c /repoze/bfg/template.py
parentbaf2406e1999160ad5d96e3adbeb525502a6d98b (diff)
downloadpyramid-ed334fdfefcdfdc570cfcd42aff171b177b76240.tar.gz
pyramid-ed334fdfefcdfdc570cfcd42aff171b177b76240.tar.bz2
pyramid-ed334fdfefcdfdc570cfcd42aff171b177b76240.zip
Render templates explicitly in views.
Diffstat (limited to 'repoze/bfg/template.py')
-rw-r--r--repoze/bfg/template.py36
1 files changed, 20 insertions, 16 deletions
diff --git a/repoze/bfg/template.py b/repoze/bfg/template.py
index c3e34c99a..4cd78f4f4 100644
--- a/repoze/bfg/template.py
+++ b/repoze/bfg/template.py
@@ -29,28 +29,32 @@ class Z3CPTTemplateFactory(object):
def package_path(package):
return os.path.abspath(os.path.dirname(package.__file__))
-def render_template(view, template_path, **kw):
+def registerTemplate(template, path):
+ try:
+ sm = getSiteManager()
+ except ComponentLookupError:
+ pass
+ else:
+ sm.registerUtility(template, IView, name=path)
+
+def render_template_explicit(path, **kw):
# XXX use pkg_resources
- if not os.path.isabs(template_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)
- template_path = os.path.join(prefix, template_path)
+ path = os.path.join(prefix, path)
- template = queryUtility(IView, template_path)
+ template = queryUtility(IView, path)
if template is None:
- if not os.path.exists(template_path):
- raise ValueError('Missing template file: %s' % template_path)
- template = Z3CPTTemplateFactory(template_path)
- try:
- sm = getSiteManager()
- except ComponentLookupError:
- pass
- else:
- sm.registerUtility(template, IView, name=template_path)
-
- return template(view=view, context=view.context, request=view.request,
- options=kw)
+ if not os.path.exists(path):
+ raise ValueError('Missing template file: %s' % path)
+ template = Z3CPTTemplateFactory(path)
+ registerTemplate(template, path)
+
+ return template(**kw)
+
+render_template = render_template_explicit