python 在 Jinja2 中,如何将宏与块标记结合使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2104957/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:47:40  来源:igfitidea点击:

In Jinja2, how can I use macros in combination with block tags?

pythondjangotemplatesdjango-templatesjinja2

提问by Rey

I'm a front end developer, and I've been trying to get a hang on using Jinja2 effectively. I want to tweak a current site so it has multiple base templates using inheritance, it fully uses block tags to substitute content and override it, and uses macros to support passing of arguments.

我是一名前端开发人员,我一直在尝试有效地使用 Jinja2。我想调整当前站点,使其具有多个使用继承的基本模板,它完全使用块标记来替换内容并覆盖它,并使用宏来支持参数传递。

My base template contains this code (edited for simplicity):

我的基本模板包含此代码(为简单起见进行了编辑):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
{% from "foo.html" import macro1, macro2, macro3 %}
{% macro base_template(title=none, arg2=none, urls={}, arg3=false) %}
<html>
  <title>{{ title }} | Site.com</title>
  ....
  {{ caller() }}
  ....
</html>
{% endmacro %}

{% block content %}{% endblock %}

And my pages that extend it look like this:

我扩展它的页面如下所示:

{% extends "base.html" %}
{% block content %}
{% call base_template(title="home", arg2="active", arg3="true") %}
(html code here)
{% endcall %}
{% endblock %}

So basically all the pages extend base, they call a macro and pass arguments to that macro. I don't quite understand it all, but the main point is that this allows default values and a degree of flexibility that doesn't require redefining an entire block: it gives some degree of flexibility and power. Again this is heavily simplified.

所以基本上所有的页面都扩展 base,它们调用一个宏并将参数传递给该宏。我不太明白这一切,但主要的一点是,这允许默认值和一定程度的灵活性,不需要重新定义整个块:它提供了一定程度的灵活性和功能。这再次被大大简化。

The only problem is, this negates my ability to use blocks. Macros are for flexibility, but with blocks, I have the ability to override something entirely, or use it's parents contents and add to it, which I can't do with Macros (at least I don't think). The problem is, I can't wrap things in blocks, else they won't see the values in the macro. For instance, doing this:

唯一的问题是,这否定了我使用块的能力。宏是为了灵活性,但是对于块,我可以完全覆盖某些内容,或者使用它的父内容并将其添加到其中,这是宏无法做到的(至少我不这么认为)。问题是,我不能用块包装东西,否则他们将看不到宏中的值。例如,这样做:

{% block title %}<title>{{ title }} | Site.com</title>{% endblock %}

Will fail because it will say title is undefined.

会失败,因为它会说标题未定义。

Ultimately I am looking for a way to utilize both the power and organiztional aspects of blocks, but still be able to utilize the logic & terseness of macros. If anyone could give me any help as to how I might go about this problem, I would really appreciate it.

最终,我正在寻找一种方法来利用块的功能和组织方面,但仍然能够利用宏的逻辑和简洁性。如果有人可以就我如何解决这个问题给我任何帮助,我将不胜感激。

回答by LeafStorm

Blocks are only definable at a template's top level. If you extend a template, any values set in the child template using a settag will be accessible from the template it is extending. For example, if you have a template named layout.html:

块只能在模板的顶层定义。如果您扩展模板,set则可以从它正在扩展的模板访问使用标记在子模板中设置的任何值。例如,如果您有一个名为 的模板layout.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
  <title>{{ title }} | Site.com</title>
  ....
  {% block content %}{% endblock content %}
  ....
</html>

And you have this child template, index.html:

你有这个子模板,index.html

{% extends "layout.html" %}
{% set title = 'Homepage' %}
{% block content %}
(html code here)
{% endblock content %}

Then the reference to titlein the parent would resolve to 'Homepage'. You can do this with any type of variable. For what you're doing, I don't think there is any need for macros - if you take advantage of this feature and place blocks well, you will be able to do pretty much everything you need to do as far as layouts are concerned. I would look at some of the templates used by Plurk Solace, which is written by one of the Jinja2 authors, if you want to get a good idea of when to use various features of Jinja2.

然后对title父项中的引用将解析为'Homepage'. 您可以使用任何类型的变量执行此操作。对于您正在做的事情,我认为不需要宏 - 如果您利用此功能并很好地放置块,就布局而言,您将能够完成几乎所有您需要做的事情. 如果您想很好地了解何时使用 Jinja2 的各种功能,我会查看Plurk Solace使用的一些模板,该模板由Jinja2 的一位作者编写。