Flask Base (+)--The reuse of template code macro, inheritance (Block), including (include)

Source: Internet
Author: User

Macro

An understanding of macros (macro):

Think of it as a function in JINJA2 that returns a template or HTML string

To avoid writing the same template code over and over again, there is code redundancy that can be written as a function for reuse.

Template code fragments that need to be reused in multiple places can be written to separate files and then included in all templates to avoid duplication

Defining macros

{% macro input (name,value= ', type= ' text ')%    } <  type= "{{type}}"  name= "{{name}}"        value= "{{value}}" "  class=" Form-control ">{% ENDMACRO%}

Calling macros

{{input (' name ' value= ' Zs ')}}

This will output

<type= "text"  namevalue= "ZS"  class  = "Form-control">

The macro is extracted separately, encapsulated into HTML files, other templates for import use, file names can be customized macro.html

{% macro function (type= ' text ', name= ', value= ')%} <  type= "{{type}}"  name= "{{name}}"value= "{{value}}" class = "Form-control" > {% Endmacro%}

In other template files, first import, and then call

{% import ' macro.html ' as func%} {% func.function ()%}
Code Walkthrough

Before using the macro code

<form>    <label>User name:</label><inputtype= "text"name= "username"><BR/>    <label>ID Number:</label><inputtype= "text"name= "Idcard"><BR/>    <label>Password:</label><inputtype= "Password"name= "Password"><BR/>    <label>Confirm Password:</label><inputtype= "Password"name= "Password2"><BR/>    <inputtype= "Submit"value= "Register"></form>

Defining macros

{#定义宏, equivalent to the definition of a function, when used directly call the macro, passing in different parameters can be #}{% macro input (label= "", type= "text", Name= "", value= "")%} < label > {{label}} </ label ><  type= "{{type}}"  name= "{{name}}"  value= "{{value}" } ">{% ENDMACRO%}

Using macros

<form>{{input ("User name:", name= "username")}}<BR/>{{input ("Social Security Number:", name= "Idcard")}}<BR/>{{input ("Password:", type= "password", name= "password")}}<BR/>{{input ("Confirm password:", type= "password", name= "Password2")}}<BR/>{{input (type= "Submit", value= "Register")}}</form>
Template Inheritance

Template inheritance is intended to reuse public content in a template. In general Web development, inheritance is primarily used at the top of the site menu, at the bottom. These can be defined in the parent template, which inherits directly from the child template without having to repeat the writing.

What the label defines

{% block top%} {% Endblock%}

is equivalent to digging a hole in the parent template that can be populated when the child template inherits from the parent template.

The child template uses the extends directive to declare which template the template inherits from

The blocks defined in the parent template are redefined in the child template, and the contents of the parent template can be called using super ()

Parent Template

Base.html

{% block top%}  Top Menu {% endblock top%}{% block content%}{% endblock content%}{% block bottom%}  Bottom {% endblock bottom%}
Child Templates

Extends directive declares where this template inherits from

{% extends ' base.html '%} {% block content%} What needs to be populated {% Endblock contents%}

Note points when template inheritance is used:

Multiple inheritance is not supported

For readability, when using extends in a child template, write as much as possible on the first line of the template.

You cannot define multiple block tags of the same name in a template file.

When using multiple block tags on a page, it is recommended to give the end tag a name, which is better read when multiple blocks are nested.

include

The JINJA2 template, in addition to macros and inheritance, also supports the functionality of code reuse, called inclusion (include). Its function is to load another template into the current template and render it directly.

Use of include

{% include ' hello.html '%}

Included when used, if the containing template file does not exist, the program throws a templatenotfound exception, which can be added with the ignore missing keyword. If the included template file does not exist, this include statement is ignored.

Use of include plus keyword ignore missing

{% include ' hello.html ' ignore missing%}
Summary
    • Macro, Inheritance (Block), including (include) can be used to implement code reuse.
    • The essence of Inheritance (Block) is code substitution, which is commonly used to implement multiple, unchanging areas of a page.
    • Macros (macro) function like functions that can pass in parameters that need to be defined, called.
    • Include is the rendering of the target template file directly.

Flask Base (+)--The reuse of template code macro, inheritance (Block), including (include)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.