mozilla / nunjucks

A powerful templating engine with inheritance, asynchronous control, and more (jinja2 inspired)

Home Page:https://mozilla.github.io/nunjucks/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature request: pass data back from included/imported templates

NVolcz opened this issue · comments

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.