Other auxiliary tags

Some commonly used built-in tags.

Nested references to templatesinclude

When creating templates, we often extract some common parts, such as header, footer, and aside, and store them independently. There is no need to repeat them on every page; it is sufficient to include them in each page.At this time, we can use the include tag.

{% include "partial/header.html" %}
{% include "partial/footer.html" %}

The `include` directive can embed a split code fragment into the complete document. The form is{% include "模板文件" %}.

If the template that needs to be introduced does not exist, it will report an error. If we don't know whether the template that needs to be introduced exists, we need to add itif_existsjudge.

{% 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 include template is imported, which inherits all variables from the current template. If you want to add additional variables to the included template, you can usewithto increase. like:

{% include "partial/header.html" with title="这是声明给header使用的title" %}

This defines the title variable for the template included, and other variables in the current template can also be used continuously.

If you need to declare multiple variables for templates included, you can use them consecutivelykey=valueThe form is increased, separated by spaces, such as:

{% include "partial/header.html" with title="这是声明给header使用的title" keywords="这是声明给header使用的keywords" %}

If you only want the include template to use specific variables instead of all the variables from the current template, you can use only to set a limit:

{% 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 functionsmacro

iris.DjangoThe 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 %}

The macro function can also be saved to an independent file and then imported to nest. When a file contains multiple macro functions, it can be used,Separate multiple macro functions by commas. You can also use `as` to set aliases:

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 %}

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 %}

Inheritance of templatesextends

The inheritance of templates is a bit like the master slide in PowerPoint, 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 that inherits it, or it will display the master template by default.

<!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 may change in a block, because a block does not affect the template parsing even if it is not rewritten on subsequent pages, and it is more convenient to rewrite when necessary.

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 variables

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 properties, 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>

Using struct body methods in templates

For example, in the archive list, the structure of archive defines built-in functionsfunc (archive *Archive) GetThumb(), then it can be called directly in the template. like:

{% 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 the built-inarchive.GetThumb()method.

Output current time in template

nowThe label provides the current time output in the template.The `now` format for time formatting follows 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" %}

loremRandomly 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 there is no real data in the development template, using this tag can quickly fill in enough random data.

-----
{% lorem %}
-----
{% lorem 10 %}
-----
{% lorem 3 p %}
-----
{% lorem 100 w %}
-----

Template comments

iris.DjangoWe use braces +# to implement comments for comments:{# 注释内容 #}

Single line comments{# 这只能注释单行 #}, use multiple lines of comments{% comment %}这里注释很多行{% endcomment %}.

Example:

Empty single line comments

{# #}

Single line comments

{# testing single line comment #}

Fill in single line comments with valid tags

{# testing single line comment {% if thing %}{% endif %} #}

Fill in single line comments with invalid tags

{# testing single line comment {% if thing %} #}

Fill in single line comments with invalid syntax

{# testing single line comment {% if thing('') %}wow{% endif %} #}

Empty block comments

{% comment %}{% endcomment %}

Fill text single line block comments

{% comment %}filled block comment {% endcomment %}

Empty multi-line block comment

{% comment %}


{% endcomment %}

Block comments with other tags

{% comment %}
  {{ thing_goes_here }}
  {% if stuff %}do stuff{% endif %}
{% endcomment %}

Block comments with invalid tags in them

{% comment %}
  {% if thing %}
{% endcomment %}

Block comments using invalid syntax

{% comment %}
  {% thing('') %}
{% endcomment %}

General labels between comments to ensure that they do not break in the lexer

{% if hello %}
{% endif %}
after if
{% comment %}All done{% endcomment %}