Jinja2 {% include file %} outside of search path doesn't work

℡╲_俬逩灬. 提交于 2020-12-07 06:46:45

问题


This is an elementary issue which is probably related to Jinja2 PrefixLoader or ChoiceLoader.

On Python 3.6 we load with this command

jinja2.FileSystemLoader( searchpath= "\\template_folder\\")

On Windows 7, our file structure is as follows.

- folder_bbb
    * subfile.txt
- template_folder
     * template_file
     - folder_aaa
         * subfile.txt

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.


回答1:


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" %} 

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.)



来源:https://stackoverflow.com/questions/50058873/jinja2-include-file-outside-of-search-path-doesnt-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!