如何在 Jinja 中创建包含模板后面的值的标签?
我正在使用 Jinja2,并且我正在尝试创建几个协同工作的标签,这样如果我有一个看起来像这样的模板:
{{ my_summary() }}
... arbitrary HTML ...
{{ my_values('Tom', 'Dick', 'Harry') }}
... arbitrary HTML ...
{{ my_values('Fred', 'Barney') }}
我最终会得到以下结果:
This page includes information about <b>Tom</b>, <b>Dick</b>, <b>Harry</b>, <b>Fred</b>, and <b>Barney</b>.
... arbitrary HTML ...
<h1>Tom, Dick, and Harry</h1>
... arbitrary HTML ...
<h1>Fred and Barney</h1>
换句话说, my_summary()页面开头包含页面稍后提供的信息。 它应该足够智能,能够考虑 include
和 import
语句中出现的表达式。
最好的方法是什么?
I'm using Jinja2, and I'm trying to create a couple tags that work together, such that if I have a template that looks something like this:
{{ my_summary() }}
... arbitrary HTML ...
{{ my_values('Tom', 'Dick', 'Harry') }}
... arbitrary HTML ...
{{ my_values('Fred', 'Barney') }}
I'd end up with the following:
This page includes information about <b>Tom</b>, <b>Dick</b>, <b>Harry</b>, <b>Fred</b>, and <b>Barney</b>.
... arbitrary HTML ...
<h1>Tom, Dick, and Harry</h1>
... arbitrary HTML ...
<h1>Fred and Barney</h1>
In other words, the my_summary() at the start of the page includes information provided later on in the page. It should be smart enough to take into account expressions which occur in include
and import
statements, as well.
What's the best way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
免责声明:我不知道 Jinja。
我的猜测是你无法(轻松)完成此任务。
我建议采用以下替代方案:
模板:
控制器:
Disclaimer: I do not know Jinja.
My guess is that you cannot (easily) accomplish this.
I would suggest the following alternative:
Template:
Controller: