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 635 forks source link

Question about carrying variables from one block to another. #1365

Open BurnyLlama opened 2 years ago

BurnyLlama commented 2 years ago

Let's say I have a template called "template.njk":

{% block A %}

{% endblock %}

{% block B %}

{% endblock %}

And "page.njk" extends it:

{% extends "template.njk" %}

{# set a variable #}
{% set foo = "bar" %}

{% block A %}
     {# use the variable #}
     {{ foo }}

     {# modify the variable #}
     {% set foo = "baz" %}
     {# I would now want this to "scope up" so I can use it in block B #}
{% endblock %}

{% block B %}
     {{ foo }}
     {# this now outputs "bar", but I want it to output "baz", how would I do that? #}
{% endblock %}

As I explained in the comments in the code, I wonder if there's a way to carry over the changed variable into the other block... Is that possible?