Skip to content
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

Closed
roll opened this issue Jun 12, 2015 · 10 comments
Closed

Overriding a block within an include #460

roll opened this issue Jun 12, 2015 · 10 comments

Comments

@roll
Copy link

roll commented Jun 12, 2015

base.html

{% include 'partial.html' %}

partial.html

{% block contents %}
partial.html
{% endblock %}

extension.html

{% extends 'base.html' %}
{% block contents %}
extension.html
{% endblock %}

Nunjucks 1.3.4 actual:

partial.html

Expected (?):

extension.html
@devoidfury
Copy link
Contributor

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?

partial.html

{% block main %} included {% endblock %}

base.html

{% include 'partial.html' %}
<main>
{% block main %} base {% endblock %}
</main>

example.html

{% extends 'base.html' %}
{% block main %} example {% endblock %}

@roll
Copy link
Author

roll commented Jun 12, 2015

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.

@devoidfury
Copy link
Contributor

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]
base

{% 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 %}

@jlongster
Copy link
Contributor

include does not create an inheritance so there are lot of complexities with how the inheritance chain for blocks should work. This is simply something I'm not interesting in, honestly, I don't think the return would be enough for the high amount of complexity it would introduce to the code.

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.

@carljm
Copy link
Contributor

carljm commented Sep 30, 2015

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.

@sbussard
Copy link

[How] Does this relate to #539 ?

@carljm
Copy link
Contributor

carljm commented Oct 29, 2015

It doesn't relate to #539, IMO.

@est
Copy link

est commented Mar 31, 2019

@candu
Copy link

candu commented Feb 20, 2021

This reminds me of React's children or Vue's slot mechanism, which in turn would suggest a syntax like:

{% 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.)

@stepanjakl
Copy link

+1 for scoping blocks within included template, like @candu suggests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants