Testing the template_context_prerender hook

This allows for modifying any context *right before render*, including
access to the variables that are passed in.  This test takes advantage
of that and takes one of the variables, "doubleme", and modifies
it (doubles it!)

In our case it turns "happy" and "joy" into "happyhappy" and "joyjoy".

This commit sponsored by Mark Holmquist.  Thank you!
This commit is contained in:
Christopher Allan Webber
2013-05-15 11:40:28 -05:00
parent 38ebd05d1a
commit a1099bba79
5 changed files with 17 additions and 5 deletions

View File

@@ -26,6 +26,11 @@ def append_to_global_context(context):
context['global_append'] = 'globally appended!'
return context
def double_doubleme(context):
if 'doubleme' in context:
context['doubleme'] = context['doubleme'] * 2
return context
def setup_plugin():
routes = [
@@ -46,4 +51,5 @@ hooks = {
'setup': setup_plugin,
('modify_context.specific_page',
'contextplugin/specific.html'): append_to_specific_context,
'template_global_context': append_to_global_context}
'template_global_context': append_to_global_context,
'template_context_prerender': double_doubleme}

View File

@@ -2,3 +2,4 @@ General page!
global thing: {{ global_append }}
lol: {{ lol }}
doubleme: {{ doubleme }}

View File

@@ -3,3 +3,4 @@ Specific page!
specific thing: {{ specific_page_append }}
global thing: {{ global_append }}
something: {{ something }}
doubleme: {{ doubleme }}

View File

@@ -21,11 +21,13 @@ def specific(request):
return render_to_response(
request,
'contextplugin/specific.html',
{"something": "orother"})
{"something": "orother",
"doubleme": "happy"})
def general(request):
return render_to_response(
request,
'contextplugin/general.html',
{"lol": "cats"})
{"lol": "cats",
"doubleme": "joy"})