2013-02-13 91 views
1

延长我有以下代码:神社负载模板 - 从文件

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

回答

1

我想你可以用这个ChoiceLoader这个?将您生成的模板放入DictLoader,然后使用DictLoader和适当的FileSystemLoader设置ChoiceLoader