Jinja : access parent variable from child

I'm using Jinja to set my newsletters system variables.

I have a common variables file (parent.j2) and every theme can override these vars (child.j2) via {% extends 'parent.j2' %}

From the parent side, I can access variables declared in the child but not the other way around, is there a way ?

Thanks :)

WORKING

parent.j2 {% set a = a|default(1) %}

child.j2 {% set a = 3 %}

(a = 3)

NOT WORKING

parent.j2 {% set a = 1 %}

child.j2 {% set b = a %}

(a is not defined)

I ended up adding an import under the extends, and it works so far.

Working example :

parent.j2

{% set foo = foo|default("foo") %}
{% set from_parent = "ok" %}

child.j2

{% extends 'parent.j2' %}
{% import 'parent.j2' as parent with context %}
{% set foo = "bar" %}
{% set to_child = parent.from_parent %}

Output :

foo : "bar"

to_child : "ok"

Back to Top