Ticket #6510: 6510.2.diff
File 6510.2.diff, 5.3 KB (added by , 15 years ago) |
---|
-
django/template/__init__.py
diff --git a/django/template/__init__.py b/django/template/__init__.py index 7fb01f0..33ea0aa 100644
a b class Node(object): 770 770 # Set this to True for nodes that must be first in the template (although 771 771 # they can be preceded by text nodes. 772 772 must_be_first = False 773 child_nodelists = ('nodelist',) 773 774 774 775 def render(self, context): 775 776 "Return the node rendered as a string" … … class Node(object): 783 784 nodes = [] 784 785 if isinstance(self, nodetype): 785 786 nodes.append(self) 786 if hasattr(self, 'nodelist'): 787 nodes.extend(self.nodelist.get_nodes_by_type(nodetype)) 787 for attr in self.child_nodelists: 788 nodelist = getattr(self, attr, None) 789 if nodelist: 790 nodes.extend(nodelist.get_nodes_by_type(nodetype)) 788 791 return nodes 789 792 790 793 class NodeList(list): -
django/template/defaulttags.py
diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py index 69afd84..4b0a4d1 100644
a b class FirstOfNode(Node): 97 97 return u'' 98 98 99 99 class ForNode(Node): 100 child_nodelists = ('nodelist_loop', 'nodelist_empty') 101 100 102 def __init__(self, loopvars, sequence, is_reversed, nodelist_loop, nodelist_empty=None): 101 103 self.loopvars, self.sequence = loopvars, sequence 102 104 self.is_reversed = is_reversed … … class ForNode(Node): 118 120 for node in self.nodelist_empty: 119 121 yield node 120 122 121 def get_nodes_by_type(self, nodetype):122 nodes = []123 if isinstance(self, nodetype):124 nodes.append(self)125 nodes.extend(self.nodelist_loop.get_nodes_by_type(nodetype))126 nodes.extend(self.nodelist_empty.get_nodes_by_type(nodetype))127 return nodes128 129 123 def render(self, context): 130 124 if 'forloop' in context: 131 125 parentloop = context['forloop'] … … class ForNode(Node): 181 175 return nodelist.render(context) 182 176 183 177 class IfChangedNode(Node): 178 child_nodelists = ('nodelist_true', 'nodelist_false') 179 184 180 def __init__(self, nodelist_true, nodelist_false, *varlist): 185 181 self.nodelist_true, self.nodelist_false = nodelist_true, nodelist_false 186 182 self._last_seen = None … … class IfChangedNode(Node): 211 207 return '' 212 208 213 209 class IfEqualNode(Node): 210 child_nodelists = ('nodelist_true', 'nodelist_false') 211 214 212 def __init__(self, var1, var2, nodelist_true, nodelist_false, negate): 215 213 self.var1, self.var2 = var1, var2 216 214 self.nodelist_true, self.nodelist_false = nodelist_true, nodelist_false … … class IfEqualNode(Node): 227 225 return self.nodelist_false.render(context) 228 226 229 227 class IfNode(Node): 228 child_nodelists = ('nodelist_true', 'nodelist_false') 229 230 230 def __init__(self, var, nodelist_true, nodelist_false=None): 231 231 self.nodelist_true, self.nodelist_false = nodelist_true, nodelist_false 232 232 self.var = var … … class IfNode(Node): 240 240 for node in self.nodelist_false: 241 241 yield node 242 242 243 def get_nodes_by_type(self, nodetype):244 nodes = []245 if isinstance(self, nodetype):246 nodes.append(self)247 nodes.extend(self.nodelist_true.get_nodes_by_type(nodetype))248 nodes.extend(self.nodelist_false.get_nodes_by_type(nodetype))249 return nodes250 251 243 def render(self, context): 252 244 if self.var.eval(context): 253 245 return self.nodelist_true.render(context) -
new file tests/regressiontests/templates/nodelist.py
diff --git a/tests/regressiontests/templates/nodelist.py b/tests/regressiontests/templates/nodelist.py new file mode 100644 index 0000000..89fac97
- + 1 from unittest import TestCase 2 from django.template.loader import get_template_from_string 3 from django.template import VariableNode 4 5 6 class NodelistTest(TestCase): 7 8 def test_for(self): 9 source = '{% for i in 1 %}{{ a }}{% endfor %}' 10 template = get_template_from_string(source) 11 vars = template.nodelist.get_nodes_by_type(VariableNode) 12 self.assertEqual(len(vars), 1) 13 14 def test_if(self): 15 source = '{% if x %}{{ a }}{% endif %}' 16 template = get_template_from_string(source) 17 vars = template.nodelist.get_nodes_by_type(VariableNode) 18 self.assertEqual(len(vars), 1) 19 20 def test_ifequal(self): 21 source = '{% ifequal x y %}{{ a }}{% endifequal %}' 22 template = get_template_from_string(source) 23 vars = template.nodelist.get_nodes_by_type(VariableNode) 24 self.assertEqual(len(vars), 1) 25 26 def test_ifchanged(self): 27 source = '{% ifchanged x %}{{ a }}{% endifchanged %}' 28 template = get_template_from_string(source) 29 vars = template.nodelist.get_nodes_by_type(VariableNode) 30 self.assertEqual(len(vars), 1) -
tests/regressiontests/templates/tests.py
diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py index 31c9e24..3e7b7fb 100644
a b from context import context_tests 24 24 from custom import custom_filters 25 25 from parser import token_parsing, filter_parsing, variable_parsing 26 26 from unicode import unicode_tests 27 from nodelist import NodelistTest 27 28 from smartif import * 28 29 29 30 try: