Applied initial merge of the new subtemplate renderer

This commit is contained in:
Roberto Rosario
2011-04-30 04:45:05 -04:00
parent f65dbb46f7
commit 16061b304e
4 changed files with 105 additions and 58 deletions
+5 -19
View File
@@ -1,5 +1,6 @@
{% extends "base.html" %}
{% load i18n %}
{% load subtemplates_tags %}
{% block title %} :: {% with "true" as read_only %}{% with "true" as striptags %}{% include "calculate_form_title.html" %}{% endwith %}{% endwith %}{% endblock %}
@@ -91,15 +92,7 @@
{% endwith %}
{% endfor %}
{% for subtemplate in subtemplates_dict %}
{% with subtemplate.title as title %}
{% with subtemplate.object_list as object_list %}
{% with subtemplate.extra_columns as extra_columns %}
{% with subtemplate.hide_object as hide_object %}
{% with subtemplate.main_object as main_object %}
{% with subtemplate.hide_link as hide_link %}
{% with subtemplate.hide_header as hide_header %}
{% with subtemplate.navigation_object_links as navigation_object_links %}
{% for subtemplate in subtemplates_list %}
<div class="grid_{{ subtemplate.grid|default:11 }}">
@@ -111,7 +104,7 @@
{% with subtemplate.form_action as form_action %}
{% with "true" as read_only %}
{% with subtemplate.form as form %}
<div class="generic_subform">
<div class="generic_subform">
{% include subtemplate.name %}
</div>
{% endwith %}
@@ -122,20 +115,13 @@
{% endwith %}
{% endwith %}
{% else %}
{% include subtemplate.name %}
{% render_subtemplate subtemplate.name subtemplate.context as rendered_subtemplate %}
{{ rendered_subtemplate }}
{% endif %}
</div>
{% if subtemplate.grid_clear or not subtemplate.grid %}
<div class="clear"></div>
{% endif %}
{% endwith %}
{% endwith %}
{% endwith %}
{% endwith %}
{% endwith %}
{% endwith %}
{% endwith %}
{% endwith %}
{% endfor %}
</div>
{% endblock %}
@@ -0,0 +1,51 @@
import re
from django.template import Node, TemplateSyntaxError, Library, \
Variable, Context
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
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, 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)
#format_string[1:-1]