Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow loading template from iterable

  • Loading branch information...
commit 0b3369355dadb39ac1ce9580d95004233031a287 1 parent 04bb720
@dmishe dmishe authored
Showing with 24 additions and 3 deletions.
  1. +5 −3 flask/templating.py
  2. +19 −0 flask/testsuite/templating.py
View
8 flask/templating.py
@@ -109,17 +109,19 @@ def _render(template, context, app):
return rv
-def render_template(template_name, **context):
+def render_template(template_name_or_list, **context):
"""Renders a template from the template folder with the given
context.
- :param template_name: the name of the template to be rendered
+ :param template_name_or_list: the name of the template to be
+ rendered, or an iterable with template names
+ the first one existing will be rendered
:param context: the variables that should be available in the
context of the template.
"""
ctx = _request_ctx_stack.top
ctx.app.update_template_context(context)
- return _render(ctx.app.jinja_env.get_template(template_name),
+ return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
context, ctx.app)
View
19 flask/testsuite/templating.py
@@ -178,6 +178,25 @@ def index():
self.assert_equal(rv.data, 'Hello Custom World!')
+ def test_iterable_loader(self):
+ app = flask.Flask(__name__)
+ @app.context_processor
+ def context_processor():
+ return {'whiskey': 'Jameson'}
+ @app.route('/')
+ def index():
+ return flask.render_template(
+ ['no_template.xml', # should skip this one
+ 'simple_template.html', # should render this
+ 'context_template.html'],
+ value=23)
+
+ rv = app.test_client().get('/')
+ self.assert_equal(rv.data, '<h1>Jameson</h1>')
+
+
+
+
def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TemplatingTestCase))
Please sign in to comment.
Something went wrong with that request. Please try again.