Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Advertising jinja2 via entry-point to TurboGears and ToscaWidgets #109

Closed
wants to merge 1 commit into from

2 participants

@ralphbean

No description provided.

@rduplain
Collaborator

I'll add a big +1 for this. Should this patch no longer apply, the ext.py is simply moved to __init__.py in the new ext package. @mitsuhiko I'd like to pull this, but want to give you a chance to comment.

@ralphbean

The associated issue has been closed.

#108

Closing this Pull Request as well.

@ralphbean ralphbean closed this
@ralphbean

Just posting here for posterity, tw2 supports jinja2 (without the buffet interface) as of tw2-2.1.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 13, 2012
  1. @ralphbean
This page is out of date. Refresh to see the latest.
View
0  jinja2/ext.py → jinja2/ext/__init__.py
File renamed without changes
View
61 jinja2/ext/entrypoints.py
@@ -0,0 +1,61 @@
+# -*- coding: utf-8 -*-
+"""
+ jinja2.ext.entrypoints
+ ~~~~~~~~~~~~~~~~~~~~~~
+
+ These Jinja extensions advertise the Jinja2 engine by way of entry points.
+ It was intended for consumption by the TurboGears and ToscaWidgets
+ frameworks but can be reused by others.
+
+ This module was lifted from Mako ext/turbogears.py
+ Copyright (C) 2006-2012 the Mako authors and contributors.
+
+ :copyright: (c) 2012 by the Jinja Team.
+ :license: BSD.
+"""
+
+import re
+import inspect
+
+from jinja2 import Template
+from jinja2 import Environment, PackageLoader
+
+
+class EPPlugin(object):
+ """ Plugin for delivering Jinja2 as an entry point """
+
+ def __init__(self, extra_vars_func=None, options=None, extension='html'):
+ self.extra_vars_func = extra_vars_func
+ self.extension = extension
+ if not options:
+ options = {}
+
+ # Pull the options out and initialize the lookup
+ lookup_options = {}
+ for k, v in options.iteritems():
+ if k.startswith('jinja2.'):
+ lookup_options[k[7:]] = v
+ elif k in ['directories', 'filesystem_checks', 'module_directory']:
+ lookup_options[k] = v
+
+ self.lookup_options = lookup_options
+
+ def load_template(self, templatename, template_string=None):
+ """Loads a template from a file or a string"""
+ if template_string is not None:
+ return Template(template_string)
+
+ toks = templatename.split('.')
+ package, folder, tname = '.'.join(toks[:-2]), toks[-2], toks[-1]
+ env = Environment(loader=PackageLoader(package, folder))
+ return env.get_template(tname + '.' + self.extension)
+
+ def render(self, info, format="html", fragment=False, template=None):
+ if isinstance(template, basestring):
+ template = self.load_template(template)
+
+ # Load extra vars func if provided
+ if self.extra_vars_func:
+ info.update(self.extra_vars_func())
+
+ return template.render(**info)
View
21 jinja2/testsuite/ext.py
@@ -16,6 +16,7 @@
from jinja2 import Environment, DictLoader, contextfunction, nodes
from jinja2.exceptions import TemplateAssertionError
from jinja2.ext import Extension
+from jinja2.ext.entrypoints import EPPlugin
from jinja2.lexer import Token, count_newlines
from jinja2.utils import next
@@ -446,10 +447,30 @@ def test_volatile_scoping(self):
assert '<testing>\\n' in pysource
+class EPPluginTestCase(JinjaTestCase):
+
+ def setup(self):
+ self.tl = EPPlugin()
+
+ def test_basic(self):
+ t = self.tl.load_template('jinja2.testsuite.res.templates.test')
+ assert t.render() == "BAR"
+
+ def test_subdir(self):
+ try:
+ t = self.tl.load_template('jinja2.testsuite.res.templates.foo.test')
+ except ImportError as e:
+ assert str(e) == "No module named templates"
+
+ def test_string(self):
+ t = self.tl.load_template('foo', "hello world")
+ assert t.render() == "hello world"
+
def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(ExtensionsTestCase))
suite.addTest(unittest.makeSuite(InternationalizationTestCase))
suite.addTest(unittest.makeSuite(NewstyleInternationalizationTestCase))
suite.addTest(unittest.makeSuite(AutoEscapeTestCase))
+ suite.addTest(unittest.makeSuite(EPPluginTestCase))
return suite
View
3  setup.py
@@ -104,6 +104,9 @@
entry_points="""
[babel.extractors]
jinja2 = jinja2.ext:babel_extract[i18n]
+
+ [python.templating.engines]
+ jinja2 = jinja2.ext.entrypoints:EPPlugin
""",
features={'debugsupport': debugsupport},
**extra
Something went wrong with that request. Please try again.