问题描述
我有两个文件:
base.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{{title}}</title>
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>
register.html
{% extends "base.html" %}
{% block content %}
<h1>Register</h1>
<form action="" method="post" name="register">
{{ form.hidden_tag() }}
{{ form.login.label }} {{ form.login(size=20) }}
{{ form.password.label }} {{ form.password(size=20) }}
<input type="submit" value="Register">
</form>
{% endblock %}
它的渲染方式如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Register</title>
</head>
<body>
<h1>Register</h1>
<form action="" method="post" name="register">
<div style="display:none;"><input id="csrf_token" name="csrf_token" type="hidden" value="1393257771.168161##ce877b3519f192c05d3b409f3b7b07bb147dead7"></div>
<label for="login">login</label> <input id="login" name="login" size="20" type="text" value="">
<label for="password">password</label> <input id="password" name="password" size="20" type="password" value="">
<input type="submit" value="Register">
</form>
</body>
</html>
我想实现这一目标:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Register</title>
</head>
<body>
<h1>Register</h1>
<form action="" method="post" name="register">
<div style="display:none;"><input id="csrf_token" name="csrf_token" type="hidden" value="1393257771.168161##ce877b3519f192c05d3b409f3b7b07bb147dead7"></div>
<label for="login">login</label> <input id="login" name="login" size="20" type="text" value="">
<label for="password">password</label> <input id="password" name="password" size="20" type="password" value="">
<input type="submit" value="Register">
</form>
</body>
</html>
我错过了什么吗?我尝试使用Google并弄乱了模板缩进,而且indent
过滤器似乎在这里不适用.我不想在内部块中对缩进进行硬编码,因为如果以后我决定更改基数中的格式和元素,那将会破坏.
尽管我认为文档大小的增加是反对正确"缩进文档的一个很好的论据,但 jinja2实际上提供了一个功能来完成您要做的事情:
indent(s, width=4, indentfirst=False)
您将在base.html
中使用一个宏,如 SO上的答案中所述.. >
无论如何,我都可能会将该宏命名为render_register_form
:
{% macro render_register_form(form) %}
<h1>Register</h1>
<form action="" method="post" name="register">
{{ form.hidden_tag() }}
{{ form.login.label }} {{ form.login(size=20) }}
{{ form.password.label }} {{ form.password(size=20) }}
<input type="submit" value="Register">
</form>
{% endmacro %}
,然后在需要的地方添加它,例如,带有8个空格的缩进,例如:
{{ render_register_form(my_form)|indent(8, True) }}
I have two files:
base.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{{title}}</title>
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>
register.html
{% extends "base.html" %}
{% block content %}
<h1>Register</h1>
<form action="" method="post" name="register">
{{ form.hidden_tag() }}
{{ form.login.label }} {{ form.login(size=20) }}
{{ form.password.label }} {{ form.password(size=20) }}
<input type="submit" value="Register">
</form>
{% endblock %}
It gets rendered like this:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Register</title>
</head>
<body>
<h1>Register</h1>
<form action="" method="post" name="register">
<div style="display:none;"><input id="csrf_token" name="csrf_token" type="hidden" value="1393257771.168161##ce877b3519f192c05d3b409f3b7b07bb147dead7"></div>
<label for="login">login</label> <input id="login" name="login" size="20" type="text" value="">
<label for="password">password</label> <input id="password" name="password" size="20" type="password" value="">
<input type="submit" value="Register">
</form>
</body>
</html>
I would like to achieve this:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Register</title>
</head>
<body>
<h1>Register</h1>
<form action="" method="post" name="register">
<div style="display:none;"><input id="csrf_token" name="csrf_token" type="hidden" value="1393257771.168161##ce877b3519f192c05d3b409f3b7b07bb147dead7"></div>
<label for="login">login</label> <input id="login" name="login" size="20" type="text" value="">
<label for="password">password</label> <input id="password" name="password" size="20" type="password" value="">
<input type="submit" value="Register">
</form>
</body>
</html>
Am I missing something? I tried Google and messing with the template indentations, also indent
filter does not seem to apply here. I do not want to hard-code the indentation in the inner block, as that would break if I decide to change the formatting and elements in the base later.
Although I think the increase in document size is a good argument against "correctly" indenting documents, jinja2 actually provides a function to do just what you what:
indent(s, width=4, indentfirst=False)
which you would use in your base.html
with a macro, as described in this answer on SO.
I would probably make that a macro anyway and call it render_register_form
:
{% macro render_register_form(form) %}
<h1>Register</h1>
<form action="" method="post" name="register">
{{ form.hidden_tag() }}
{{ form.login.label }} {{ form.login(size=20) }}
{{ form.password.label }} {{ form.password(size=20) }}
<input type="submit" value="Register">
</form>
{% endmacro %}
and then include it where needed, for example, with 8 spaces indentation, as:
{{ render_register_form(my_form)|indent(8, True) }}
这篇关于Jinja2正确缩进包含的块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!