Jinja 模板中是否允许内联代码?
我在我的网站上使用 Jinja,我喜欢它。
我遇到了一个简单的需求。如何显示今天的日期? 有没有办法在 Jinja 模板中内联一些 Python 代码?
import datetime
now = datetime.datetime.utcnow()
print now.strftime("%Y-%m-%d %H:%M")
本文说不,但建议使用宏或过滤器?
真的吗?我们必须诉诸所有这些吗?好的,在这种情况下会是什么样子?
I'm using Jinja on my site and I like it.
I've come across a simple need. How to display today's date? Is there a way to inline some Python code in a Jinja template?
import datetime
now = datetime.datetime.utcnow()
print now.strftime("%Y-%m-%d %H:%M")
This article says no, but suggests using a macro or a filter?
Really? Must we resort to all that? OK, what would that look like in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,没有办法将 Python 内联到 Jinja 中。但是,您可以通过扩展模板的环境来添加 Jinja 知道的构造引擎或适用于所有模板的全局命名空间。或者,您可以添加一个过滤器来设置日期时间对象的格式。
Flask 将 Jinja2 环境存储在
app.jinja_env
。您可以通过直接添加到此字典或使用@ 来将新上下文注入到环境中app.context_processor
装饰器。无论您选择什么路径,都应该在设置应用程序时、在处理任何请求之前完成。 (请参阅网站的片段部分,了解一些好的如何设置过滤器的示例 - 文档包含添加到全局变量的一个很好的示例)。
No, there is no way to inline Python into Jinja. However, you can add to the constructs that Jinja knows by extending the Environment of the template engine or the global namespace available to all templates. Alternately, you can add a filter that let's you format datetime objects.
Flask stores the Jinja2 Environment on
app.jinja_env
. You can inject new context into the environment by either adding to this dictionary directly, or by using the@app.context_processor
decorator.Whatever path you choose, this should be done while you are setting up the application, before you have served any requests. (See the snippets section of the website for some good examples of how to set up filters - the docs contain a good example of adding to the global variables).
目前的答案几乎适用于所有情况。然而,在极少数情况下,您可能希望在模板中包含 Python 代码。就我而言,我想用它来预处理一些乳胶文件,并且我更愿意将生成表值、绘图等的 python 代码保留在乳胶文件中。
所以我做了一个 Jinja2 扩展,添加了一个新的“py”块,允许在模板内编写 python 代码。请记住,我必须采取一些有问题的解决方法才能使其正常工作,因此我不能 100% 确定它在哪些情况下会失败或表现异常。
这是一个示例模板。
如果我们将模板参数设置为 foo=10,change_foo=True ,则输出为:
带有用于运行示例的 main 函数的扩展。
The current answers are correct for pretty much every situation. However there are some very rare cases where you would want to have python code inside the template. In my case I want to use it to preprocess some latex files and I would prefer to keep the python code generating table values, plots, etc, inside the latex file it self.
So I made a Jinja2 extension that adds a new "py" block allowing python code to be written inside the template. Please keep in mind that I had to do some questionable work-arounds to get this to work, so I'm not 100% sure in which situations it fails or behaves unexpectedly.
This is an example template.
The output if we set the template parameters to
foo=10, change_foo=True
is:The extension with a main function to run the example.
您可以添加全局变量,这些变量可以从 Jinja 模板访问。您可以将自己的函数定义放在那里,它可以执行您需要的任何操作。
You can add to global variables which can be accessed from Jinja templates. You can put your own function definitions in there, which do whatever you need.