【问题标题】:Jinja load template from string - and extend from fileJinja 从字符串加载模板 - 并从文件扩展
【发布时间】:2013-01-29 15:22:09
【问题描述】:

我有以下代码:

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 文档来看,这似乎是不可行的。有什么解决方法吗?

【问题讨论】:

    标签: python django django-templates jinja2


    【解决方案1】:

    我认为您可以为此使用ChoiceLoader 吗?将生成的模板放入DictLoader,然后使用DictLoader 和适当的FileSystemLoader 设置ChoiceLoader

    【讨论】:

      猜你喜欢
      • 2017-01-15
      • 2011-01-11
      • 2011-10-15
      • 1970-01-01
      • 1970-01-01
      • 2016-12-03
      • 2015-11-24
      • 2018-05-26
      • 2012-05-27
      相关资源
      最近更新 更多