首页 > 解决方案 > Jinja 使用变量作为 Jinja “包含”中使用的文件的名称

问题描述

在 jinja 中,我试图将变量传递到“include”使用的文件名中,例如:{% include = variable1 %}

像这样的东西:

{% set filepath = ['Templates/file1.j2', 'Templates/file2.j2', 'Templetes/file3.j2'] %}
These are the files:
{% for list in filepath -%}
   '{{ list }}'
{% endfor %} 

{% set Mypath = filepath[1] %}
This is the path and file to use: '{{ Mypath }}'

{% include "'" Mypath "'" %}

我希望达到的结果应该是这样的。

{% include = 'Templates/file2.j2' %}.

是否可以在 Jinja 中以这种方式将变量传递给包含?

标签: includejinja2

解决方案


推荐阅读