TaiwanGrapefruitTea
TaiwanGrapefruitTea

Reputation: 1245

Jinja2 macro import "with context" and global variables: {% from file.html import macro_name with context %}

In Jinja2 template engine for python, my macro "with context" could not access the global variables from the context.

Import statements:

{% import 'en-US/my_macro.html' as  all_macros %}
{% from 'en-US/my_macro.html' import  the_macro  with context  %}

Macro usage:

{{ all_macros.the_macro() }}

The macro is being called, but the globals have no value.

Here's the relevant docs for "with context"

Upvotes: 5

Views: 6848

Answers (1)

TaiwanGrapefruitTea
TaiwanGrapefruitTea

Reputation: 1245

The issue arises because there are 2 ways to call the macro. One is from the all_macros import. The second is with the name the_macro.

If I use the call:

{{ the_macro() }}

Then the macros have access to the global variables from the context! This is really a clarification of the documentation.

Upvotes: 7

Related Questions