Permalink
Browse files

First stab at jinja2 support.

  • Loading branch information...
ralphbean committed Apr 28, 2012
1 parent 4d16358 commit 17d17234ac00d12aad6e4c4de1e5a3a9f1e06469
@@ -30,6 +30,7 @@ def get_description(fname='README.rst'):
# Requirements to install buffet plugins and engines
_extra_genshi = ["Genshi >= 0.3.5"]
_extra_mako = ["Mako >= 0.1.1"]
_extra_jinja = ["jinja2"]
setup(
name='tw2.core',
@@ -52,11 +53,12 @@ def get_description(fname='README.rst'):
'FormEncode',
'WebTest',
'strainer',
] + _extra_genshi + _extra_mako,
] + _extra_genshi + _extra_mako + _extra_jinja,
test_suite = 'nose.collector',
extras_require = {
'genshi': _extra_genshi,
'mako': _extra_mako,
'jinja': _extra_jinja,
},
url = "http://toscawidgets.org/",
download_url = "http://toscawidgets.org/download/",
@@ -6,7 +6,7 @@
from nose.tools import raises, eq_
# TBD: only test engines that are installed
engines = ['genshi', 'mako']
engines = ['genshi', 'mako', 'jinja']
class TestWD(twc.Widget):
@@ -112,11 +112,10 @@ def test_widget_display(self):
twc.core.request_local()['middleware'] = twc.make_middleware(None)
mtest = TestWD(id='x')
for eng in engines:
print eng
test = mtest.req()
test.template = '%s:tw2.core.test_templates.inner_%s' % (eng, eng)
out = test.display()
assert(out == '<p>TEST bob</p>')
eq_(out, '<p>TEST bob</p>')
def test_widget_nesting(self):
twc.core.request_local()['middleware'] = twc.make_middleware(None)
@@ -73,7 +73,7 @@ class Config(object):
`preferred_rendering_engines`
List of rendering engines in order of preference.
(default: ['mako','genshi'])
(default: ['mako','genshi','jinja'])
`strict_engine_selection`
If set to true, TW2 will only select rendering engines from within your
@@ -83,7 +83,7 @@ class Config(object):
`rendering_engine_lookup`
A dictionary of file extensions you expect to use for each type of
template engine.
(default: {'mako':'mak','genshi':'html'})
(default: {'mako':['mak', 'mako'],'genshi':['html'],'jinja':['html']})
`script_name`
A name to prepend to the url for all resource links (different from
@@ -106,11 +106,12 @@ class Config(object):
validator_msgs = {}
encoding = 'utf-8'
auto_reload_templates = None
preferred_rendering_engines = ['mako', 'genshi']
preferred_rendering_engines = ['mako', 'genshi', 'jinja']
strict_engine_selection = True
rendering_extension_lookup = {
'mako': 'mak',
'genshi': 'html',
'mako': ['mak', 'mako'],
'genshi': ['html'],
'jinja': ['jinja', 'html'],
}
script_name = ''
@@ -137,21 +138,6 @@ def __init__(self, **kw):
if self.auto_reload_templates is None:
self.auto_reload_templates = self.debug
self.available_rendering_engines = {}
for e in iter_entry_points("python.templating.engines"):
if not self.strict_engine_selection or \
e.name in self.preferred_rendering_engines:
try:
self.available_rendering_engines[e.name] = e.load()
except DistributionNotFound:
pass
# test to see if the rendering engines are available for the preferred
# engines selected
for engine_name in self.preferred_rendering_engines:
if engine_name not in self.available_rendering_engines:
self.preferred_rendering_engines.remove(engine_name)
class TwMiddleware(object):
"""ToscaWidgets middleware
@@ -14,6 +14,7 @@
'mako': ['mak', 'mako'],
'genshi': ['html'],
'genshi_abs': ['html'], # just for backwards compatibility with tw2 2.0.0
'jinja': ['jinja', 'html'],
}
@@ -27,19 +28,15 @@ def get_engine_name(template_name, mw=None):
if ':' in template_name:
engine_name = template_name.split(':', 1)[0]
engine_name_cache[template_name] = engine_name
try:
return engine_name_cache[template_name]
except KeyError:
pass
return engine_name
try:
if mw is None:
rl = core.request_local()
mw = rl['middleware']
pref_rend_eng = mw.config.preferred_rendering_engines
except (KeyError, AttributeError):
pref_rend_eng = ['mako', 'genshi']
pref_rend_eng = ['mako', 'genshi', 'jinja']
# find the first file in the preffered engines available for templating
for engine_name in pref_rend_eng:
@@ -51,7 +48,7 @@ def get_engine_name(template_name, mw=None):
pass
if not mw.config.strict_engine_selection:
pref_rend_eng = ['mako', 'genshi']
pref_rend_eng = ['mako', 'genshi', 'jinja']
for engine_name in pref_rend_eng:
try:
get_source(engine_name, template_name)
@@ -109,6 +106,11 @@ def get_render_callable(engine_name, displays_on, src):
import genshi.template
tmpl = genshi.template.MarkupTemplate(src)
return lambda kwargs: literal(tmpl.generate(**kwargs))
elif engine_name == 'jinja':
import jinja2
tmpl = jinja2.Template(src)
return lambda kwargs: literal(tmpl.render(**kwargs))
raise NotImplementedError("Unhandled engine")
@@ -0,0 +1 @@
<p>TEST {{w.test}}</p>
@@ -0,0 +1 @@
<p>TEST jinja</p>
@@ -0,0 +1 @@
<p>TEST {{test}}</p>
@@ -0,0 +1 @@
<p>TEST {{w.children.y.display()}}</p>
@@ -37,6 +37,7 @@
rendering_extension_lookup = {
'mako': 'mak',
'genshi': 'html',
'jinja': 'html',
}
rm = pk.ResourceManager()
@@ -91,7 +91,7 @@ class Widget(pm.Parametered):
)
inline_engine_name = pm.Param(
'Name of an engine. If specified, `template` is interpreted as ' +
'an *inline template* and not a path. Only "mako" is supported.',
'an *inline template* and not a path.',
default=None,
)
validator = pm.Param(

0 comments on commit 17d1723

Please sign in to comment.