Modernize the render_subtemplate template tag

This commit is contained in:
Roberto Rosario
2015-04-06 02:11:15 -04:00
parent 50b7019ace
commit 920ed6601a

View File

@@ -1,53 +1,19 @@
from __future__ import unicode_literals
import re
from django.template import (
Context, Library, Node, TemplateSyntaxError, Variable
)
from django.template import Context, Library
from django.template.loader import get_template
register = Library()
class RenderSubtemplateNode(Node):
def __init__(self, template_name, template_context, var_name):
self.template_name = template_name
self.template_context = template_context
self.var_name = var_name
@register.assignment_tag(takes_context=True)
def render_subtemplate(context, template_name, template_context):
"""
Renders the specified template with the mixed parent and
subtemplate contexts
"""
def render(self, context):
template_name = Variable(self.template_name).resolve(context)
template_context = Variable(self.template_context).resolve(context)
new_context = Context(context)
new_context.update(Context(template_context))
return get_template(template_name).render(new_context)
new_context = Context(context)
new_context.update(Context(template_context, autoescape=context.autoescape))
csrf_token = context.get('csrf_token', None)
if csrf_token is not None:
new_context['csrf_token'] = csrf_token
context[self.var_name] = get_template(template_name).render(new_context)
return ''
@register.tag
def render_subtemplate(parser, token):
# This version uses a regular expression to parse tag contents.
try:
# Splitting by None == splitting by spaces.
tag_name, arg = token.contents.split(None, 1)
except ValueError:
raise TemplateSyntaxError('%r tag requires arguments' % token.contents.split()[0])
m = re.search(r'(.*?) (.*?) as (\w+)', arg)
if not m:
raise TemplateSyntaxError('%r tag had invalid arguments' % tag_name)
template_name, template_context, var_name = m.groups()
if (template_name[0] == template_name[-1] and template_name[0] in ('"', "'")):
raise TemplateSyntaxError('%r tag\'s template name argument should not be in quotes' % tag_name)
if (template_context[0] == template_context[-1] and template_context[0] in ('"', "'")):
raise TemplateSyntaxError('%r tag\'s template context argument should not be in quotes' % tag_name)
return RenderSubtemplateNode(template_name, template_context, var_name)