Ticket #6510: 6510.diff
File 6510.diff, 5.7 KB (added by , 16 years ago) |
---|
-
django/template/__init__.py
739 739 # Set this to True for nodes that must be first in the template (although 740 740 # they can be preceded by text nodes. 741 741 must_be_first = False 742 child_nodelists = ('nodelist',) 742 743 743 744 def render(self, context): 744 745 "Return the node rendered as a string" … … 752 753 nodes = [] 753 754 if isinstance(self, nodetype): 754 755 nodes.append(self) 755 if hasattr(self, 'nodelist'): 756 nodes.extend(self.nodelist.get_nodes_by_type(nodetype)) 756 for attr in self.child_nodelists: 757 nodelist = getattr(self, attr, None) 758 if nodelist: 759 nodes.extend(nodelist.get_nodes_by_type(nodetype)) 757 760 return nodes 758 761 759 762 class NodeList(list): -
django/template/defaulttags.py
8 8 except NameError: 9 9 from django.utils.itercompat import reversed # Python 2.3 fallback 10 10 11 from django.template import Node, NodeList, Template, Context,Variable11 from django.template import Node, NodeList, Template, Variable 12 12 from django.template import TemplateSyntaxError, VariableDoesNotExist, BLOCK_TAG_START, BLOCK_TAG_END, VARIABLE_TAG_START, VARIABLE_TAG_END, SINGLE_BRACE_START, SINGLE_BRACE_END, COMMENT_TAG_START, COMMENT_TAG_END 13 13 from django.template import get_library, Library, InvalidTemplateLibrary 14 14 from django.conf import settings … … 83 83 return u'' 84 84 85 85 class ForNode(Node): 86 child_nodelists = ('nodelist_loop',) 87 86 88 def __init__(self, loopvars, sequence, is_reversed, nodelist_loop): 87 89 self.loopvars, self.sequence = loopvars, sequence 88 90 self.is_reversed = is_reversed … … 98 100 for node in self.nodelist_loop: 99 101 yield node 100 102 101 def get_nodes_by_type(self, nodetype):102 nodes = []103 if isinstance(self, nodetype):104 nodes.append(self)105 nodes.extend(self.nodelist_loop.get_nodes_by_type(nodetype))106 return nodes107 108 103 def render(self, context): 109 104 nodelist = NodeList() 110 105 if 'forloop' in context: … … 190 185 return '' 191 186 192 187 class IfEqualNode(Node): 188 child_nodelists = ('nodelist_true', 'nodelist_false') 189 193 190 def __init__(self, var1, var2, nodelist_true, nodelist_false, negate): 194 191 self.var1, self.var2 = Variable(var1), Variable(var2) 195 192 self.nodelist_true, self.nodelist_false = nodelist_true, nodelist_false … … 212 209 return self.nodelist_false.render(context) 213 210 214 211 class IfNode(Node): 212 child_nodelists = ('nodelist_true', 'nodelist_false',) 213 215 214 def __init__(self, bool_exprs, nodelist_true, nodelist_false, link_type): 216 215 self.bool_exprs = bool_exprs 217 216 self.nodelist_true, self.nodelist_false = nodelist_true, nodelist_false … … 226 225 for node in self.nodelist_false: 227 226 yield node 228 227 229 def get_nodes_by_type(self, nodetype):230 nodes = []231 if isinstance(self, nodetype):232 nodes.append(self)233 nodes.extend(self.nodelist_true.get_nodes_by_type(nodetype))234 nodes.extend(self.nodelist_false.get_nodes_by_type(nodetype))235 return nodes236 237 228 def render(self, context): 238 229 if self.link_type == IfNode.LinkTypes.or_: 239 230 for ifnot, bool_expr in self.bool_exprs: -
tests/regressiontests/templates/tests.py
732 732 # Inheritance from a template that doesn't have any blocks 733 733 'inheritance27': ("{% extends 'inheritance26' %}", {}, 'no tags'), 734 734 735 # Base template, putting block in a conditional {% if %} tag 736 'inheritance28': ("1{% if optional %}{% block opt %}2{% endblock %}{% endif %}3", {'optional': True}, '123'), 737 738 # Inherit from a template with block wrapped in an {% if %} tag (in parent), still gets overridden 739 'inheritance29': ("{% extends 'inheritance28' %}{% block opt %}two{% endblock %}", {'optional': True}, '1two3'), 740 'inheritance30': ("{% extends 'inheritance28' %}{% block opt %}two{% endblock %}", {}, '13'), 741 742 # Base template, putting block in a conditional {% ifequal %} tag 743 'inheritance31': ("1{% ifequal optional 1 %}{% block opt %}2{% endblock %}{% endifequal %}3", {'optional': 1}, '123'), 744 745 # Inherit from a template with block wrapped in an {% ifequal %} tag (in parent), still gets overridden 746 'inheritance32': ("{% extends 'inheritance31' %}{% block opt %}two{% endblock %}", {'optional': 1}, '1two3'), 747 'inheritance33': ("{% extends 'inheritance31' %}{% block opt %}two{% endblock %}", {'optional': 2}, '13'), 748 749 # Base template, putting block in a {% for %} tag 750 'inheritance34': ("{% for n in numbers %}_{% block for %}{{ n }}{% endblock %}{% endfor %}_", {'numbers': '123'}, '_1_2_3_'), 751 752 # Inherit from a template with block wrapped in an {% for %} tag (in parent), still gets overridden 753 'inheritance35': ("{% extends 'inheritance34' %}{% block for %}X{% endblock %}", {'numbers': '123'}, '_X_X_X_'), 754 'inheritance36': ("{% extends 'inheritance34' %}{% block for %}X{% endblock %}", {}, '_'), 755 735 756 ### I18N ################################################################## 736 757 737 758 # {% spaceless %} tag