mozilla / nunjucks

A powerful templating engine with inheritance, asynchronous control, and more (jinja2 inspired)
https://mozilla.github.io/nunjucks/
BSD 2-Clause "Simplified" License
8.48k stars 634 forks source link

Feature request: pass data back from included/imported templates #1435

Open NVolcz opened 1 year ago

NVolcz commented 1 year ago

It is sometimes useful to be able to pass data back from the parent template when dealing with included and/or imported child templates.

My specific use case:

Parent:

{% set footnotes = ["that means that", "the other thing refers to that"] %}
{% set content = "this, that[^1] the other thing[^2]" %}
<section class="content">
{% include "content.njk" %}
</section>
<section class="footnotes">
{% foreach reference in references %}
<div>{{ footnotes[reference - 1] }}</div>
{% endfor %}
</section>

Child (content.njk):

{# I have some complex macro here to render the content which finds and formats references #}
{% set references = [1,2] %}
{{ content | markdown }}

Would be nice if there were a {% include "content.njk" allow-writing-to-context %} or some other way to allow a child template to pass data back through the context.