在Jinja2中,如何将宏与块标签结合使用? [英] In Jinja2, how can I use macros in combination with block tags?

查看:461
本文介绍了在Jinja2中,如何将宏与块标签结合使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名前端开发人员,而且我一直在试图有效地使用Jinja2。我想调整一个当前的网站,所以它有多个基础模板使用继承,它完全使用块标签替代内容并覆盖它,并使用宏来支持传递参数。

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.

我的基本模板包含此代码(为简单起见):

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.

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

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.

推荐答案

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

Blocks are only definable at a template's top level. If you extend a template, any values set in the child template using a set tag will be accessible from the template it is extending. For example, if you have a template named 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>

你有这个子模板, index.html

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

然后,父项中引用标题将解析为'Homepage'。您可以使用任何类型的变量来执行此操作。对于你在做什么,我认为没有任何宏需要 - 如果你利用这个功能,并且把块放在一起,你可以做很多关于布局所需的一切事情。我将看看 Plurk Solace 使用的一些模板,其中由Jinja2作者之一撰写,如果您想了解何时使用Jinja2的各种功能。

Then the reference to title in 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.

这篇关于在Jinja2中,如何将宏与块标签结合使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆