Ticket #6510: 6510.diff

File 6510.diff, 5.7 KB (added by SmileyChris, 7 years ago)
  • django/template/__init__.py

     
    739739    # Set this to True for nodes that must be first in the template (although
    740740    # they can be preceded by text nodes.
    741741    must_be_first = False
     742    child_nodelists = ('nodelist',)
    742743
    743744    def render(self, context):
    744745        "Return the node rendered as a string"
     
    752753        nodes = []
    753754        if isinstance(self, nodetype):
    754755            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))
    757760        return nodes
    758761
    759762class NodeList(list):
  • django/template/defaulttags.py

     
    88except NameError:
    99    from django.utils.itercompat import reversed     # Python 2.3 fallback
    1010
    11 from django.template import Node, NodeList, Template, Context, Variable
     11from django.template import Node, NodeList, Template, Variable
    1212from 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
    1313from django.template import get_library, Library, InvalidTemplateLibrary
    1414from django.conf import settings
     
    8383        return u''
    8484
    8585class ForNode(Node):
     86    child_nodelists = ('nodelist_loop',)
     87
    8688    def __init__(self, loopvars, sequence, is_reversed, nodelist_loop):
    8789        self.loopvars, self.sequence = loopvars, sequence
    8890        self.is_reversed = is_reversed
     
    98100        for node in self.nodelist_loop:
    99101            yield node
    100102
    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 nodes
    107 
    108103    def render(self, context):
    109104        nodelist = NodeList()
    110105        if 'forloop' in context:
     
    190185        return ''
    191186
    192187class IfEqualNode(Node):
     188    child_nodelists = ('nodelist_true', 'nodelist_false')
     189
    193190    def __init__(self, var1, var2, nodelist_true, nodelist_false, negate):
    194191        self.var1, self.var2 = Variable(var1), Variable(var2)
    195192        self.nodelist_true, self.nodelist_false = nodelist_true, nodelist_false
     
    212209        return self.nodelist_false.render(context)
    213210
    214211class IfNode(Node):
     212    child_nodelists = ('nodelist_true', 'nodelist_false',)
     213
    215214    def __init__(self, bool_exprs, nodelist_true, nodelist_false, link_type):
    216215        self.bool_exprs = bool_exprs
    217216        self.nodelist_true, self.nodelist_false = nodelist_true, nodelist_false
     
    226225        for node in self.nodelist_false:
    227226            yield node
    228227
    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 nodes
    236 
    237228    def render(self, context):
    238229        if self.link_type == IfNode.LinkTypes.or_:
    239230            for ifnot, bool_expr in self.bool_exprs:
  • tests/regressiontests/templates/tests.py

     
    732732            # Inheritance from a template that doesn't have any blocks
    733733            'inheritance27': ("{% extends 'inheritance26' %}", {}, 'no tags'),
    734734
     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
    735756            ### I18N ##################################################################
    736757
    737758            # {% spaceless %} tag
Back to Top