Some commonly used built-in tags.
Nested references in templatesinclude
When creating templates, we often extract some common parts, such as header, footer, aside, and store them independently. We do not need to repeat writing them on each page, just need to include them on each page.At this point, we can use the include tag.
{% include "partial/header.html" %}
{% include "partial/footer.html" %}
The `include` directive can embed a split code fragment (fragment) into a complete document. The usage form is{% include "模板文件" %}
.
If the template to be introduced does not exist, it will report an error, such as if we do not know whether the template to be introduced exists, we need to addif_exists
judgment.
{% include "partial/header.html" if_exists %}
If the header.html template exists, it will be included. If it does not exist, it will not cause an error; it will just be ignored.
By default, the templates included by include will inherit all variables of the current template. If you want to add additional variables to the template included by include, you can usewith
Increase it. For example:
{% include "partial/header.html" with title="这是声明给header使用的title" %}
This defines the title variable for the template included by include, and other variables in the current template can also be used continuously.
If you need to declare multiple variables for the templates included, you can use them consecutivelykey=value
in the form, separated by spaces, such as:
{% include "partial/header.html" with title="这是声明给header使用的title" keywords="这是声明给header使用的keywords" %}
If you want to restrict the variables used by templates included with only, rather than all variables of the current template, you can use only to do so:
{% include "partial/header.html" with title="这是声明给header使用的title" keywords="这是声明给header使用的keywords" only %}
Then use it in the header.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{title}}</title>
<meta name="keywords" content="{{keywords}}">
</head>
Template code snippet macro functionmacro
iris.Django
The template engine can easily define some macro function code snippets.The macro code snippet is equivalent to a function, which can only call variables passed in as parameters.Similar to using include.However, macro has a limited scope.macro
:
定义一个宏函数
{% macro archive_detail(archive) %}
<li class="item">
<a href="/archive/{{archive.Id}}" class="link">
<h5 class="title">{{archive.Title}}</h5>
</a>
</li>
{% endmacro %}
使用定义的宏函数
{% for item in archives %}
{{ archive_detail(item) }}
{% endfor %}
Macros can also be saved to separate files and then imported to be nested. When a file contains multiple macro functions, you can use,
Separate consecutive macro function introductions. You can also use as to set an alias:
Save macro functions to archive.helper
{% macro archive_detail(archive) %}
<li class="item">
<a href="/archive/{{archive.Id}}" class="link">
<h5 class="title">{{archive.Title}}</h5>
</a>
</li>
{% endmacro %}
{% macro archive_detail2(archive) %}
<li class="item">
<a href="/archive/{{archive.Id}}" class="link">
<h5 class="title">{{archive.Title}}</h5>
</a>
</li>
{% endmacro %}
Introduce in index.html:
用import引入:
{% import "archive.helper" archive_detail, archive_detail2 as archive_detail_new, archive_detail as new_item %}
调用:
{% for item in archives %}
{{ archive_detail(item) }}
{{ archive_detail_new(item) }}
{{ new_item(item) }}
{% endfor %}
Template inheritanceextends
The inheritance of templates is a bit like the master in PowerPoint, where we define a skeleton, write an entire page, most of which does not need to be changed, and the parts that need to be changed are wrapped with block tags:
{% block title %}
<title>base</title> <!-- 如果扩写了就是扩写的,不扩写就还是用base -->
{% endblock %}
The benefit of this definition is that you can override this block in the template that inherits it, or it will display as the master if not overridden.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
{% block title %}
<title>base</title> <!-- 如果扩写了就是扩写的,不扩写就还是用base -->
{% endblock %}
<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<style>
* {
margin: 0;
padding: 0;
}
.header {
width: 100%;
height: 50px;
background-color: #369;
}
</style>
</head>
<body>
<div class="header"></div>
<div class="container">
<div class="row">
<div class="col-md-3">
{% include 'aside.html' %}
</div>
<div class="col-md-9">
{% block content %}
<h4>content</h4>
{% endblock %}
</div>
</div>
</div>
</body>
</html>
Then inherit this base.html in the index.html
{% extends 'base.html' %}
{% block title %}
<title>index</title>
{% endblock %}
{% block content %}
<div class="col-md-9">
<h3>index</h3>
<p>index content</p>
</div>
{% endblock %}
This is using base.html as the master template and rewriting the title and content parts in index.html.
Note: If you use the {% extends %} tag in a template, it must be the first tag in the template.In any other case, template inheritance will not work.
In the case of inheritance, try to wrap all data that might change within a block. Because a block even if it is not rewritten on subsequent pages does not affect the template parsing, and it is more convenient to rewrite when necessary.
Similarly, if you write to a certain block later and find that multiple pages need to use it, then at this time, you should add it to base.html, making it a part of the master template.
The output of the variable
The key to iterating complex data structures in Django templates is the dot character.
The variable boundary definition is double curly braces. There is an object called people, which has Name, Gender, and Level attributes, and it is output in the template as follows:
<ul>
<li>网站:{{siteName}}</li>
<li>名字:{{people.Name}}</li>
<li>性别:{{people.Gender}}</li>
<li>等级:{{people.Level}}</li>
</ul>
Use struct structure built-in methods in the template
For example, in the archive list, the structure of archive defines built-in functionsfunc (archive *Archive) GetThumb()
Then, the value can be directly called in the template. For example:
{% for item in archives %}
<li class="item">
<a href="/archive/{{item.Id}}" class="link">
<img src="{{item.GetThumb()}}" alt="{{item.Title}}" />
<h5 class="title">{{item.Title}}</h5>
</a>
</li>
{% endfor %}
The template can be used directly{{item.GetThumb()}}
To call built-inarchive.GetThumb()
methods.
Output the current time in the template
now
Tags provide the current time output in the template.The `now` function's time formatting parameters follow the Go time formatting rules.If the fake parameter is added, it will output a specific added time instead of the current time.
{% now "Mon Jan 2 15:04:05 -0700 MST 2006" fake %}
{% now "2006-01-02 15:04" %}
lorem
Randomly generated Latin sample data
Display random "lorem ipsum" Latin text.This is very useful for providing sample data in templates.That is also placeholder content.When developing templates without real data, using this tag can quickly fill in enough random data.
-----
{% lorem %}
-----
{% lorem 10 %}
-----
{% lorem 3 p %}
-----
{% lorem 100 w %}
-----
Comments of the template
iris.Django
Comments, we use curly braces +# to implement comments:{# 注释内容 #}
Single-line comments use{# 这只能注释单行 #}
Multi-line comments use{% comment %}这里注释很多行{% endcomment %}
.
Example:
Empty single-line comment
{# #}
Single-line comment
{# testing single line comment #}
Fill single-line comment with valid tags
{# testing single line comment {% if thing %}{% endif %} #}
Fill single-line comment with invalid tags
{# testing single line comment {% if thing %} #}
Fill single-line comment with invalid syntax
{# testing single line comment {% if thing('') %}wow{% endif %} #}
Empty block comment
{% comment %}{% endcomment %}
Fill text for single-line block comment
{% comment %}filled block comment {% endcomment %}
Empty multi-line block comment
{% comment %}
{% endcomment %}
Prevent comments with other tags
{% comment %}
{{ thing_goes_here }}
{% if stuff %}do stuff{% endif %}
{% endcomment %}
Prevent comments containing invalid tags
{% comment %}
{% if thing %}
{% endcomment %}
Prevent comments using invalid syntax
{% comment %}
{% thing('') %}
{% endcomment %}
Standard tags between comments to ensure they do not disrupt the lexer
{% if hello %}
{% endif %}
after if
{% comment %}All done{% endcomment %}