搜索路径之外的Jinja2 {%包含文件%}不起作用 [英] Jinja2 {% include file %} outside of search path doesn't work
问题描述
这是一个基本问题,可能与Jinja2 PrefixLoader或ChoiceLoader有关.
This is an elementary issue which is probably related to Jinja2 PrefixLoader or ChoiceLoader.
在Python 3.6上,我们使用此命令加载
On Python 3.6 we load with this command
jinja2.FileSystemLoader( searchpath= "\\template_folder\\")
在Windows 7上,我们的文件结构如下.
On Windows 7, our file structure is as follows.
- folder_bbb
* subfile.txt
- template_folder
* template_file
- folder_aaa
* subfile.txt
在template_file中,使用此命令成功完成
From the template_file we are successful with this command
{% include "folder_aaa/subfile.txt" %}
现在我们希望将文件上移并写入
Now we wish to move the file one level up, and write
{% include "../folder_bbb/subfile.txt" %}
但这不起作用,找不到抱怨的文件.
but that doesn't work, complaining file not found.
正确的书写方式是什么?谢谢.
What is the correct way to write? Thanks.
推荐答案
您可以在加载程序中指定所有路径
You may specify all paths in the the loader
jinja2.FileSystemLoader(["c:\\template_folder\\", "c:\\folder_bbb\\"])
并引用没有特定路径的包含块
and refer the including block without a specific path
{% include "subfile.txt" %}
将按顺序搜索路径,以便如您所说,将文件上移一个级别,即可找到该文件. (您需要模板本身的template_folder路径.)
The path will be searched in order so that as you say, moving the file one level up, the file will be found. (You need the template_folder path for the template itself.)
这篇关于搜索路径之外的Jinja2 {%包含文件%}不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!