{{super()}}中的replace块 [英] replace block within {{ super() }}
问题描述
我有一个基本模板,其中包含默认<head>
内容的块.在标题块中,有一个<title>
块.
例如,在基本文件中,我将拥有:
<head>
{% block head %}
{% block title %}<title>An App</title>{% endblock title %}
<script src="somescript.js"></script>
{% endblock head %}
</head>
在子模板中,我想包括从底部开始的头栏中的所有内容(通过调用{{ super()) }}
并包括一些其他内容,但同时在超级调用中替换标题栏. /p>
是否有办法做到这一点,而不仅仅是在其余的标题内容(标题除外)周围加一个方块,并替换掉所有这些内容?
请勿调用super
.在您的子模板中,您可以执行以下操作:
{% extends "base.html" %}
{% block title %}<title>This is my new TITLE</title>{% endblock %}
Jinja用子项中定义的块替换了父项中的所有块,如果不提供新定义,它将使用父项中的定义.因此它将呈现为:
<head>
<title>TITLE</title>
<script src="somescript.js"></script>
</head>
如果要在父级中使用该块的默认值,请调用super
:
{% extends "base.html" %}
{% block title %}<title>TITLE</title>{{ super() }}{% endblock %}
这呈现为:
<head>
<title>TITLE</title><title>An App</title>
<script src="somescript.js"></script>
</head>
如果要添加更多脚本,只需在基本模板中放置一个占位符块即可:
<head>
{% block head %}
{% block title %}<title>An App</title>{% endblock title %}
<script src="somescript.js"></script>
{% block moreScripts %}{% endblock moreScripts %}
{% endblock head %}
</head>
并按以下方式使用它:
{% extends "base.html" %}
{% block title %}<title>TITLE</title>{% endblock %}
{% block moreScripts %}
<script src="somescript1.js"></script>
<script src="somescript2.js"></script>
<script src="somescript3.js"></script>
{% endblock moreScripts %}
I have a base template which includes a block for the default <head>
content. Within the head block, there's a block for the <title>
.
For example, in the base file I would have:
<head>
{% block head %}
{% block title %}<title>An App</title>{% endblock title %}
<script src="somescript.js"></script>
{% endblock head %}
</head>
In the child template, I would like to include everything that was in the head block from the base (by calling {{ super()) }}
and include some additional things, yet at the same time replace the title block within the super call.
Is there a way to do this without just putting a block around the rest of the head content (excluding the title) and just replacing all of that?
Don't call super
. In your child template you can do:
{% extends "base.html" %}
{% block title %}<title>This is my new TITLE</title>{% endblock %}
Jinja replaces all the blocks in the parent by the ones defined in the child, if you do not provide a new definition it uses the definition in the parent. So it will render as:
<head>
<title>TITLE</title>
<script src="somescript.js"></script>
</head>
You call super
if you want the default value of the block in the parent:
{% extends "base.html" %}
{% block title %}<title>TITLE</title>{{ super() }}{% endblock %}
And this renders as:
<head>
<title>TITLE</title><title>An App</title>
<script src="somescript.js"></script>
</head>
If you want to add more scripts just make a place holder block in your base template:
<head>
{% block head %}
{% block title %}<title>An App</title>{% endblock title %}
<script src="somescript.js"></script>
{% block moreScripts %}{% endblock moreScripts %}
{% endblock head %}
</head>
And use it as in :
{% extends "base.html" %}
{% block title %}<title>TITLE</title>{% endblock %}
{% block moreScripts %}
<script src="somescript1.js"></script>
<script src="somescript2.js"></script>
<script src="somescript3.js"></script>
{% endblock moreScripts %}
这篇关于{{super()}}中的replace块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!