nunjucks: extends, defined block multiple time

file1.html

{% block test %}
  file1
{% endblock %}

file2.html

{% extends "file1.html" %}
{% if true %}
  {% block test %}
    file2 block1
  {% endblock %}
{% else %}
  {% block test %}
    file2 block2
  {% endblock %}
{% endblock %}

expected: file2 block 1 actual: file2 block 2

the precompiled js-file for file2.html contains the function for the test-block twice with the same name so the last defined instance of the block get called regardless of what case the if falls into.

About this issue

  • Original URL
  • State: open
  • Created 8 years ago
  • Comments: 18 (6 by maintainers)

Commits related to this issue

Most upvoted comments

this now throws but doesn’t implement self, makes it impossible to render the same block two times (and used to work)

Same problem, I used this to render a block twice, and now it breaks.

The bug allowed for same-file block inheritance (e.g. breaking apart a template into multiple blocks in the same file, each one executing in the same context of the file’s “master” block) which seemed like handy and intended behavior.

Sorry for the upgrade surprise, everyone. If I’d thought it through more carefully, I’d have delayed this fix to 3.0.

I still think the previous behavior was a bug. There’s no reasonable interpretation of multiple definitions of the same block that applies to all possible inheritance situations, and the prior behavior was accidental and arbitrary (“last one wins”), silently dropping one of the definitions in some cases. So IMO this change is going to be in 3.0 regardless, and you may want to start thinking about alternate approaches now. But I could consider a 2.5.1 release that reverts the change for now.