Ticket #19890: 19890-fix-double-ifchanged-rendering.patch

File 19890-fix-double-ifchanged-rendering.patch, 2.2 KB (added by Diederik van der Boor, 12 years ago)
  • django/template/defaulttags.py

    diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py
    index a9a992f..6e2ea7c 100644
    a b class IfChangedNode(Node):  
    216216        if self not in state_frame:
    217217            state_frame[self] = None
    218218
     219        nodelist_true_output = None
    219220        try:
    220221            if self._varlist:
    221222                # Consider multiple parameters.  This automatically behaves
    class IfChangedNode(Node):  
    223224                compare_to = [var.resolve(context, True) for var in self._varlist]
    224225            else:
    225226                # The "{% ifchanged %}" syntax (without any variables) compares the rendered output.
    226                 compare_to = self.nodelist_true.render(context)
     227                compare_to = nodelist_true_output = self.nodelist_true.render(context)
    227228        except VariableDoesNotExist:
    228229            compare_to = None
    229230
    230231        if compare_to != state_frame[self]:
    231232            state_frame[self] = compare_to
    232             return self.nodelist_true.render(context)
     233            return nodelist_true_output or self.nodelist_true.render(context)  # render true block if not already rendered
    233234        elif self.nodelist_false:
    234235            return self.nodelist_false.render(context)
    235236        return ''
  • tests/regressiontests/templates/tests.py

    diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py
    index 6f435f2..a9ef34e 100644
    a b class Templates(TestCase):  
    426426        output1 = template.render(Context({'foo': range(3), 'gen': gen()}))
    427427        self.assertEqual(output1, '[0,1,2,3]', 'Expected [0,1,2,3] in first template, got {0}'.format(output1))
    428428
     429    def test_ifchanged_render_once(self):
     430        template = Template('[0{% for x in foo %},{% ifchanged %}{{ gen.next }}{% endifchanged %}{% endfor %}]')
     431        def gen():
     432            for i in xrange(1,10):
     433                yield i
     434
     435        output = template.render(Context({'foo': range(3), 'gen': gen()}))
     436        self.assertEqual(output, '[0,1,2,3]')
     437
    429438    def test_templates(self):
    430439        template_tests = self.get_template_tests()
    431440        filter_tests = filters.get_filter_tests()
Back to Top