jinja2 templating support
This commit is contained in:
@@ -91,6 +91,37 @@ try:
|
||||
except ImportError: # pragma no cover
|
||||
pass
|
||||
|
||||
#
|
||||
# Jinja2 rendering engine
|
||||
#
|
||||
try:
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
from jinja2 import exceptions as jinja_exceptions
|
||||
|
||||
class JinjaRenderer(object):
|
||||
def __init__(self, path, extra_vars):
|
||||
self.env = Environment(loader=FileSystemLoader(path))
|
||||
self.extra_vars = extra_vars
|
||||
|
||||
def render(self, template_path, namespace):
|
||||
template = self.env.get_template(template_path)
|
||||
return template.render(self.extra_vars.make_ns(namespace))
|
||||
_builtin_renderers['jinja'] = JinjaRenderer
|
||||
|
||||
def format_jinja_error(exc_value):
|
||||
import cgi
|
||||
if isinstance(exc_value, (jinja_exceptions.TemplateSyntaxError)):
|
||||
retval = '<h4>Jinja2 template syntax error in \'%s\' on line %d</h4>' % (exc_value.name, exc_value.lineno)
|
||||
lines = [cgi.escape(x) for x in open(exc_value.filename)]
|
||||
lineno = exc_value.lineno - 1 # files are 1 not 0 index
|
||||
lines[lineno] = '<strong>%s</strong>' % lines[lineno]
|
||||
|
||||
retval +='<div style="background-color:#ccc;padding:2em;">%s</div>' % '<br>'.join(lines)
|
||||
return retval
|
||||
error_formatters.append(format_jinja_error)
|
||||
except ImportError: # pragma no cover
|
||||
pass
|
||||
|
||||
#
|
||||
# Extra Vars Rendering
|
||||
#
|
||||
|
||||
11
tests/templates/jinja.html
Normal file
11
tests/templates/jinja.html
Normal file
@@ -0,0 +1,11 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Hello, {{name}}!</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Hello, {{name}}!</h1>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
12
tests/templates/jinja_bad.html
Normal file
12
tests/templates/jinja_bad.html
Normal file
@@ -0,0 +1,12 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Hello, {{name}}!</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Hello, {{name}}!</h1>
|
||||
</body>
|
||||
{% block %}
|
||||
|
||||
</html>
|
||||
@@ -706,6 +706,33 @@ class TestEngines(object):
|
||||
r = app.get('/index.html?name=World')
|
||||
assert r.status_int == 200
|
||||
assert "<h1>Hello, World!</h1>" in r.body
|
||||
|
||||
def test_jinja(self):
|
||||
if 'jinja' not in builtin_renderers:
|
||||
return
|
||||
class RootController(object):
|
||||
@expose('jinja:jinja.html')
|
||||
def index(self, name='Jonathan'):
|
||||
return dict(name=name)
|
||||
|
||||
@expose('jinja:jinja_bad.html')
|
||||
def badtemplate(self):
|
||||
return dict()
|
||||
|
||||
app = TestApp(Pecan(RootController(), template_path=self.template_path))
|
||||
r = app.get('/')
|
||||
assert r.status_int == 200
|
||||
assert "<h1>Hello, Jonathan!</h1>" in r.body
|
||||
|
||||
error_msg = None
|
||||
try:
|
||||
r = app.get('/badtemplate.html')
|
||||
except Exception, e:
|
||||
for error_f in error_formatters:
|
||||
error_msg = error_f(e)
|
||||
if error_msg:
|
||||
break
|
||||
assert error_msg is not None
|
||||
|
||||
def test_mako(self):
|
||||
if 'mako' not in builtin_renderers:
|
||||
@@ -724,7 +751,6 @@ class TestEngines(object):
|
||||
assert r.status_int == 200
|
||||
assert "<h1>Hello, Jonathan!</h1>" in r.body
|
||||
|
||||
app = TestApp(Pecan(RootController(), template_path=self.template_path))
|
||||
r = app.get('/index.html?name=World')
|
||||
assert r.status_int == 200
|
||||
assert "<h1>Hello, World!</h1>" in r.body
|
||||
|
||||
Reference in New Issue
Block a user