diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py
index a9a992f..6e2ea7c 100644
a
|
b
|
class IfChangedNode(Node):
|
216 | 216 | if self not in state_frame: |
217 | 217 | state_frame[self] = None |
218 | 218 | |
| 219 | nodelist_true_output = None |
219 | 220 | try: |
220 | 221 | if self._varlist: |
221 | 222 | # Consider multiple parameters. This automatically behaves |
… |
… |
class IfChangedNode(Node):
|
223 | 224 | compare_to = [var.resolve(context, True) for var in self._varlist] |
224 | 225 | else: |
225 | 226 | # 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) |
227 | 228 | except VariableDoesNotExist: |
228 | 229 | compare_to = None |
229 | 230 | |
230 | 231 | if compare_to != state_frame[self]: |
231 | 232 | 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 |
233 | 234 | elif self.nodelist_false: |
234 | 235 | return self.nodelist_false.render(context) |
235 | 236 | return '' |
diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py
index 6f435f2..a9ef34e 100644
a
|
b
|
class Templates(TestCase):
|
426 | 426 | output1 = template.render(Context({'foo': range(3), 'gen': gen()})) |
427 | 427 | self.assertEqual(output1, '[0,1,2,3]', 'Expected [0,1,2,3] in first template, got {0}'.format(output1)) |
428 | 428 | |
| 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 | |
429 | 438 | def test_templates(self): |
430 | 439 | template_tests = self.get_template_tests() |
431 | 440 | filter_tests = filters.get_filter_tests() |