python - Jinja 从字符串加载模板 - 并从文件扩展

标签 python django django-templates jinja2

我有以下代码:

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 ,然后设置ChoiceLoaderDictLoader和适当的FileSystemLoader .

关于python - Jinja 从字符串加载模板 - 并从文件扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14864416/

相关文章:

python-3.x - Django 模板中包含的 Pygal svg 图表的工具提示问题

python - 如何在 matplotlib 中创建引脚图

javascript - Django:在表单提交上发出 POST 请求而不重新加载页面或渲染到另一个页面?

django - 迁移时表 "auth_user"中不存在错误 key (user_id)=(1)

python - Django表单数据回复与显示

django - DRY 方法将类添加到基本模板中的 <body> 中?

Python 3 utf-8 编码好像不对?

javascript - Python 爬虫机械化/javascript

python - 通过保存另一列来应用 pd.Series

python - 如何对 "chess order"中的数组或查询集进行排序?