Skip to content

Instantly share code, notes, and snippets.

@c4urself
Created October 1, 2011 18:43
Show Gist options
  • Save c4urself/1256474 to your computer and use it in GitHub Desktop.
Save c4urself/1256474 to your computer and use it in GitHub Desktop.
Alternative ssr template
def alternate_ssr_template(ssr_template, default_args_pos):
"""
Decorator which changes the template and context for non-ajax
server-side requests.
This decorator MUST be first because it uses func_defaults.
`ssr_template`: Replacement template.
`default_args_pos`: The position of the 'template' argument that will
be replaced in the list of default arguments
for the function.
"""
def caller(func):
def wrapper(*args, **kwargs):
"""
We take the default template used in Ajax requests and
set it as a context variable. The template variable for the
function is then changed to the ssr template, and
the changed function is returned.
"""
request = args[0]
if not request.is_ajax():
new_ctx = {'template_name': func.func_defaults[default_args_pos]}
kwargs['ctx'] = new_ctx
kwargs['template'] = ssr_template
return func(*args, **kwargs)
return wrapper
return caller
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment