Python 如何直接从文件系统加载 jinja 模板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38642557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to load jinja template directly from filesystem
提问by Juan Tomas
The jinja API document at pocoo.orgstates:
The simplest way to configure Jinja2 to load templates for your application looks roughly like this:
配置 Jinja2 以加载应用程序模板的最简单方法大致如下所示:
from jinja2 import Environment, PackageLoader
env = Environment(loader=PackageLoader('yourapplication', 'templates'))
This will create a template environment with the default settings and a loader that looks up the templates in the templatesfolder inside the yourapplicationpython package.
这将创建一个具有默认设置的模板环境和一个在yourapplicationpython 包内的模板文件夹中查找模板的加载器。
As it turns out, this isn't so simple because you have to make/install a python package with your templates in it, which introduces a lot of needless complexity, especially if you have no intention of distributing your code. You can refer to SO questions on the topic hereand here, but the answers are vague and unsatisfying.
事实证明,这并不是那么简单,因为您必须制作/安装一个包含模板的 python 包,这会引入很多不必要的复杂性,尤其是在您无意分发代码的情况下。您可以在此处和此处参考有关该主题的 SO 问题,但答案含糊不清且令人不满意。
What a naive newbie wants to do, obviously, is just load the template directly from the filesystem, not as a resource in a package. How is this done?
显然,一个天真的新手想要做的只是直接从文件系统加载模板,而不是作为包中的资源。 这是怎么做的?
回答by Juan Tomas
Here's how: use a FileSystemLoader
instead of a PackageLoader
. I found examples on the web hereand here. Let's say you have a python file in the same dir as your template:
方法如下:使用 aFileSystemLoader
而不是 a PackageLoader
。我在这里和这里在网上找到了例子。假设您在与模板相同的目录中有一个 python 文件:
./index.py
./template.html
This index.py will find the template and render it:
这个 index.py 将找到模板并渲染它:
#!/usr/bin/python
import jinja2
templateLoader = jinja2.FileSystemLoader(searchpath="./")
templateEnv = jinja2.Environment(loader=templateLoader)
TEMPLATE_FILE = "template.html"
template = templateEnv.get_template(TEMPLATE_FILE)
outputText = template.render() # this is where to put args to the template renderer
print(outputText)
It turns out, the jinja2 API doc does have a section which discusses all the built-in loaders, so it's kind of embarrassing not to have noticed that right away. But the introduction is worded such that PackageLoader
seems to be the default, "simplest" method. For newcomers to python, this can lead to a wild goose chase.
事实证明,jinja2 API 文档确实有一个部分讨论了所有内置加载器,所以没有立即注意到这一点有点尴尬。但是介绍的措辞PackageLoader
似乎是默认的“最简单”方法。对于 Python 的新手来说,这可能会导致一场野鹅追逐。
回答by Cas
A simpler way is to directly call the jinj2.Template
constructor and use open
to load the file:
更简单的方法是直接调用jinj2.Template
构造函数并使用open
加载文件:
from jinja2 import Template
with open('template.html.jinja2') as file_:
template = Template(file_.read())
template.render(name='John')
回答by bcarroll
Here is the one liner:
这是一个班轮:
template = Template(open('template_file.j2').read())
Then you can render the template on another line, or for all in one line:
然后您可以在另一行或在一行中渲染模板:
rendered = Template(open('template_file.j2').read()).render(var="TEXT")