Some commonly used built-in tags.
Nested reference in templates.include
When creating templates, we often extract some common parts, such as header, footer, aside, etc., and store them independently, so that they do not need to be repeated on each page, and can be simply included on each page.At this time, we can use the include tag.
{% include "partial/header.html" %}
{% include "partial/footer.html" %}
include you can embed a split code fragment (fragment) into the complete document. The format is{% include "模板文件" %}.
If the template to be imported does not exist, it will report an error, such as when we do not know whether the template to be imported exists, we need to addif_existsjudgment.
{% 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, but it will be ignored.
By default, the template included, it inherits all the variables of the current template, and if you want to add additional variables to the included template, you can usewithIncrease. For example:
{% include "partial/header.html" with title="这是声明给header使用的title" %}
This defines the title variable introduced by include in the template, and other variables in the current template can also continue to be used.
If you need to declare multiple variables for the template included by include, you can use them consecutivelykey=valueare added in the form, separated by spaces, such as:
{% include "partial/header.html" with title="这是声明给header使用的title" keywords="这是声明给header使用的keywords" %}
If you only want to let the include templates use specified variables instead of all variables of the current template, you can use only to limit it:
{% include "partial/header.html" with title="这是声明给header使用的title" keywords="这是声明给header使用的keywords" only %}
Then use it in 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.DjangoThe template engine can easily define some macro function code snippets.A macro code snippet is equivalent to a function that can only call variables passed in as parameters.Similar to using include. However, macros have a limited scope.As for the article, we use the article item in the article listmacro:
定义一个宏函数
{% 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 %}
Simultaneously, macro functions can also be saved to separate files and then imported in to nest. When a file contains multiple macro functions, you can use,Separate multiple macro functions introduced consecutively. You can also use as to set an alias:
Save macro function 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 %}
Include 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 template inheritance is a bit like a master in PPT, where we define a skeleton, write a whole 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 advantage of this definition is that you can overwrite this block in the template it inherits from, or it will display according to the master template.For example, we defined a base.html
<!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 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 may change in a block, because even if the block is not rewritten on subsequent pages, it does not affect the template parsing, and it is more convenient when it needs to be rewritten.
Similarly, if you write to a certain section later and find that multiple pages need to use it, then at this time, you should add it to base.html to make it part of the master template.
Output of the variable
The key to iterating over complex data structures in Django templates is the dot character., the variable output boundary is defined by double curly braces. There is an object called people, which has Name, Gender, and Level attributes, and it is output in the template as:
<ul>
<li>网站:{{siteName}}</li>
<li>名字:{{people.Name}}</li>
<li>性别:{{people.Gender}}</li>
<li>等级:{{people.Level}}</li>
</ul>
In the template, use the struct structure built-in method
For example, in the archive list, the structure of archive defines built-in functionsfunc (archive *Archive) GetThumb()Then, it 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()Method.
Output the current time in the template
nowThe tag provides the current time in the template. The parameters for time formatting follow the golang time formatting rules.If the fake parameter is added, it will output a specific time added instead of the current time. For example:
{% now "Mon Jan 2 15:04:05 -0700 MST 2006" fake %}
{% now "2006-01-02 15:04" %}
loremRandomly generate Latin sample data
Display random "lorem ipsum" Latin text.This is very useful for providing sample data in the template. It is also placeholder content.When developing a template without real data, you can use this tag to quickly fill in a sufficient amount of random data. For example:
-----
{% lorem %}
-----
{% lorem 10 %}
-----
{% lorem 3 p %}
-----
{% lorem 100 w %}
-----
Template comments
iris.DjangoComments are implemented using curly braces+# to comment:{# 注释内容 #}
Single-line comments are used{# 这只能注释单行 #}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 single line block comment with text
{% comment %}filled block comment {% endcomment %}
Block comment is empty
{% comment %}
{% endcomment %}
Block comment with other tags is blocked
{% comment %}
{{ thing_goes_here }}
{% if stuff %}do stuff{% endif %}
{% endcomment %}
Block comment with invalid tags is blocked
{% comment %}
{% if thing %}
{% endcomment %}
Block comment blocked with invalid syntax
{% comment %}
{% thing('') %}
{% endcomment %}
Comments between the conventional tags to ensure they do not break in the lexer
{% if hello %}
{% endif %}
after if
{% comment %}All done{% endcomment %}