我有以下代码:
from jinja2 import Template
templ_str = "lots of garbly html" # Compiled by external logic
templ_context = ast.literal_eval(context_dir) # Compiled by external logic
template = Template(templ_str)
template.render(**context)
模板的主体是动态编译的并传递给模板引擎。但是,我想使用文件中的基本包装器来扩展该模板。我知道这可以使用 FileSystemLoader 来完成,但我需要从内存而不是文件加载主模板。
从 API 文档来看,这似乎是不可行的。有什么解决方法吗?
最佳答案
我认为你可以使用ChoiceLoader
为了这?将生成的模板放入 DictLoader
,然后设置ChoiceLoader
与 DictLoader
和适当的FileSystemLoader
.
关于python - Jinja 从字符串加载模板 - 并从文件扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14864416/