How to enable macros / other templates using FunctionLoader in Jinja2?

I am trying to use the Jinja2 sandbox to process templates.

I tried to use both DictLoader and FunctionLoader, but I continue to encounter similar problems ... I mainly deal with FunctionLoader.

I am unable to execute includeor importanother template (which contains macros). The FloadLoader function specified by "load" is never called for reference templates.

I tried with no luck:

  • just expecting the import to go to the loader using the basic syntax of "import" and "include"
  • passing the bootloader into context, seeing if it can pull in this way
  • passing a pattern into the template, also hoping that it can draw
  • a few more things, all of which I forgot

I am sure there must be a way to support this - can someone point me in the right direction?

+5
source share
1 answer

The import syntax should use quoted strings.

Poorly:

{% import utils %}
{% import utils.macros as macros %}
{% from utils.macros import macro_1 , macro_2 %}

Good:

{% import "utils" as utils %}
{% import "utils.macros" as macros %}
{% from "utils.macros" import macro_1 , macro_2 %}

The quoted string is passed in FunctionLoaderor used as a key withDictLoader

+15
source

All Articles