0
votes

I'm trying to refacto some pretty heavy template with jinja2 and I'm stucked on an include.

This is the behaviour i'm expecting :

<h1>{{ key }} </h1>
{% set file = key | include_text %}
{% include file %}

The custom filter returns a string like this one ::

texts/my_include.html

But instead I got this error:

jinja2.exceptions.TemplatesNotFound: Tried to select from an empty list of templates

Some hack I've already tried :

  • Place the templates in the same folder and remove the 'texts/' from the returned string
  • Add the path in the Env loader

But it keeps sending this error

I'm now wondering if jinja2 allows this implementation or if I'll have to keep this template the way it was (even if it takes a very long time to be generated).

Does someone know about some trick here ?

1

1 Answers

1
votes

Well, for those who eventually met this problem in the futur, I've solved it by removing the unecessary single quotes and by sending some empty file from my custom filter when the condition is not verified... (my mistake)

Here is my custom filter :

  @environmentfilter
  def include_text(ctx, key):
    res_dict = {
      'key_value_1' : 'file_name_1',
      'key_value_2' : 'file_name_2'
    }
    try:
      return "texts/" + res_dict[key] + ".html"
    except KeyError:
      return "texts/empty.html"

Now, the first solution I was trying works fine.