Jinja2 中的多级模板继承?
我以 html/css 为职业,并且一直作为模板设计师参与 django 项目。我目前正在开发一个使用 Jinja2 的网站,我已经使用它大约两周了。我刚刚通过阅读文档发现 Jinja2 不支持多级模板继承,因为每次渲染不能执行多个模板继承
{% extends "foo" %}
。现在我非常确定您可以在 Django 中执行此操作,Django 非常强大,因为您可以指定一个基本模板,基于该模板指定 3 或 4 个模板,然后使用这些基本模板构建页面的主要内容。难道继承的意义不是让你有更多的能力进行抽象,这样你就只能真正搞乱独特的代码吗?
无论如何,我不知道在这里做什么。我不知道是否有某种方法可以像 Django 模板一样工作。我不完全是 Django 或 Jinja(2) 方面的专家,但我可以提供所需的任何信息。
I do html/css by trade, and I have been working on and off django projects as a template designer. I'm currently working on a site that uses Jinja2, which I have been using for about 2 weeks. I just found out through reading the documentation that Jinja2 doesn't support multiple level template inheritance, as in you can't do more than one
{% extends "foo" %}
per rendering. Now I'm pretty sure you can do this in Django, which is powerful because you can specify a base template, specify 3 or 4 templates based on that, and then build the meat of your pages using those base templates. Isn't the point of inheritance so you have more power to abstract so your only really messing with unique code?
In any case I have no idea what to do here. I don't know if there is some way I can do it that will work as well as it could with the Django templates. I'm not exactly an expert at either Django or Jinja(2) but I can provide any information needed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
使用 jinja2 实现多级模板化的最佳方法之一是使用“include”
假设您将“base_layout.html”作为基本模板
,然后您希望将“child_layout.html”扩展为“base_layout.html”。
现在您的页面可以扩展“child_layout.html”,并且它将同时具有 base_layout.html 和 child_layout.html
One of the best way to achieve multiple level of templating using jinja2 is to use 'include'
let say you have 'base_layout.html' as your base template
and then you want to have 'child_layout.html' that extends 'base_layout.
and now your page can just extends 'child_layout.html' and it will have both base_layout.html and child_layout.html
从文档的措辞来看,它似乎不支持深度继承 (n) 层。
我不知道这只是一条规则,规定每个模板 1 个扩展......我现在知道了,在 jinja irc 频道的一些帮助下。
The way the documentation worded it, it seemed like it didn't support inheritance (n) levels deep.
I didn't know it was just a rule saying 1 extends per template.... I now know, with some help from the jinja irc channel.
试试这个,这对我有用,感谢@Ixm 的回答。
base.html
content.html
footer.html
并调用
Try this, this work for me thanks to @Ixm answer.
base.html
content.html
footer.html
and call with
经过一番苦苦挣扎,我在jinja2模板中找到了
{{super}}
进行多级继承。以下内容的灵感来自https://stackoverflow.com/a/31093830/1300775。
base.html
layer-1.html
layer-2.html
渲染模板
layer-2.html
将输出标题
块中的品牌 - 部分 - 文章
。After struggling for a long time, I found
{{super}}
for multiple levels of inheritance in jinja2 templates.The following is inspired from https://stackoverflow.com/a/31093830/1300775.
base.html
layer-1.html
layer-2.html
Rendering template
layer-2.html
will outputBrand - Section - Article
in blocktitle
.我最近遇到了同样的问题。我想继承几个子模板并且它起作用了。为了说明这一点,我想向您展示一个对我有用的解决方案:
我有一个包含块内容并由manage.html扩展的base.html文件。并且manage.html有一个由internet_market.html扩展的块sub_manage,所以从视觉上看它看起来像:
当我渲染它时,everythink工作正常,这意味着你可以在一个渲染中拥有多个{% extends %}。唯一的问题是,如果您使用 css 或 js 文件的相对链接,那么它可能不起作用,而是会渲染,但找不到您的 css/js 文件。
例如:
在这种情况下,您必须使用 url_for 来使用动态链接。喜欢:
I recently faced the same issue. I wanted to inherit several child templates and it worked. To illustrate it I would like to show you a solution that worked for me:
I had a base.html file that has block content and extended by manage.html. and that manage.html has a block sub_manage which is extended by internet_market.html, so visually it looks like:
when I rendered it, everythink worked fine, which means that you can have several {% extends %} in one render. the only thing is that if you are using relative links to your css or js files then it might not work, rather it will render, but it won't find your css/js files.
like:
In that case you have to use dynamic links by using url_for. like:
请参阅文档扩展,包括 和 导入。
这提供了从多个文件获取功能以用于不同目的的方法,并且与嵌套的深度不同。
您可以完美地拥有一个扩展模板的模板,扩展模板的模板......
See the documentation extending, including, and importing.
This provides the means of getting functionality from multiple files for different purposes and is different from the depth of the nesting.
You can perfectly have a template that extends a template that extends a template...
多重继承和多级继承并不相同。我知道这个问题与后者有关。
让我展示一下我的问题解决方法:
login.html 和 home.html 都使用父模板中的所有数据,但只有 home.html 使用子模板(导航栏)中的数据。
Multiple inheritance and multiple-level inheritance are not the same. I understand the question is related to the latter.
Let me show my workaround for the problem:
Both login.html and home.html uses all the data from parent-template, but only home.html uses data from child-template (the navbar).
您可以使用以下方式将不同的内容组合到一个layout.html中以进行各种布局设计:
调用: 。
...并在python代码中
You could use the following way to combine different contents into a single layout.html for various layout designs:
...and call:
in python code.