-
Notifications
You must be signed in to change notification settings - Fork 641
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Overriding a block within an include #460
Comments
I'm not sure that using blocks in includes is a good idea -- how should the block resolution be handled? For example, what should happen here?
{% block main %} included {% endblock %}
{% include 'partial.html' %}
<main>
{% block main %} base {% endblock %}
</main>
{% extends 'base.html' %}
{% block main %} example {% endblock %} |
I suppose the same as here: {% block main %} included {% endblock %}
<main>
{% block main %} base {% endblock %}
</main> It's more a question than a bug report but I thought about includes like macroses to build a template on the first step. May be it works the other way internally. |
I think you're on to something, and that it's something that should be discussed further. There are some complexities to it, however -- how to handle blocks defined in includes in top level templates, blocks in includes inside top level template blocks[1], blocks in includes in child templates, error messages when there's a block name conflict in included templates where the block should be top level in both places. [1] {% block main %}{% include 'partial' %}{% endblock %} partial {% block extra %}extra partial {% endblock %} child {% extends 'base' %}
{% block main %}
// some code
{{ super() }}
{% endblock %}
{% block extra %}extra extended{% endblock %} |
Please re-open if you really want to see this. I can label is as an enhancement request if there is further discussion. Closing for now. |
Not only would this be complex, but it would also break existing uses. Right now an included template has its own totally separate inheritance hierarchy, so it can extend its own base and override blocks from there. So making included templates participate in the inheritance hierarchy of the parent template instead would be a major change that would break existing templates. |
[How] Does this relate to #539 ? |
It doesn't relate to #539, IMO. |
is this a bug in JInja2? Because this seems related |
This reminds me of React's {% include 'component' %}
{% block main %}
<!-- block content here -->
{% endblock %}
{% endinclude %} which avoids the name collision issue by scoping blocks within the context of the included template. (I don't know how parser internals work here, though, so no idea how feasible this would be - and at some point, you might just be better off using SSR support in React / Vue to perform static site generation, heh.) |
+1 for scoping blocks within included template, like @candu suggests |
base.html
partial.html
extension.html
Nunjucks 1.3.4 actual:
Expected (?):
The text was updated successfully, but these errors were encountered: