diff options
author | Christopher Allan Webber <cwebber@dustycloud.org> | 2011-06-04 16:32:13 -0500 |
---|---|---|
committer | Christopher Allan Webber <cwebber@dustycloud.org> | 2011-06-04 16:33:19 -0500 |
commit | e9279f21376feb5c43675c31b6f25e9fabac2ac6 (patch) | |
tree | 708ddb4bc1f705eab7cbe60fa7a8da01ac1dda57 | |
parent | c5678c1ab3deb3f7a2961225c35260d5bbd69604 (diff) | |
download | mediagoblin-e9279f21376feb5c43675c31b6f25e9fabac2ac6.tar.lz mediagoblin-e9279f21376feb5c43675c31b6f25e9fabac2ac6.tar.xz mediagoblin-e9279f21376feb5c43675c31b6f25e9fabac2ac6.zip |
Added new render_template method which will make our lives nicer and
also will be useful for unit testing purposes :)
-rw-r--r-- | mediagoblin/util.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/mediagoblin/util.py b/mediagoblin/util.py index fdb2c3f5..41f8a92a 100644 --- a/mediagoblin/util.py +++ b/mediagoblin/util.py @@ -78,6 +78,33 @@ def get_jinja_env(template_loader, locale): return template_env +# We'll store context information here when doing unit tests +TEMPLATE_TEST_CONTEXT = {} + + +def render_template(request, template, context): + """ + Render a template with context. + + Always inserts the request into the context, so you don't have to. + Also stores the context if we're doing unit tests. Helpful! + """ + template = request.template_env.get_template( + template) + context['request'] = request + rendered = template.render(context) + + if TESTS_ENABLED: + TEMPLATE_TEST_CONTEXT[template] = context + + return rendered + + +def clear_test_template_context(): + global TEMPLATE_TEST_CONTEXT + TEMPLATE_TEST_CONTEXT = {} + + def setup_user_in_request(request): """ Examine a request and tack on a request.user parameter if that's |