Table of Contents
- 1. Introduction
- 2. Usage
- 3. Parameters and Variables
- 4. Precautions
- 4.1. Macro can only be used for the following three kinds of variables:
- 4.2. Relationship with Block:
- 5. Reference documentation
1 Introduction
JINJA2 is the template engine for Python, and in the process of writing Python web use, it is found that macro can save a lot of code. So make a small summary.
Usage is basically an online article. I mainly write a note, but also is the experience.
2 Usage
Macros are similar to functions in general programming languages. They are used as reusable functions in place of commonly used behaviors, instead of manual duplication of work. Example:
{% macro input (name, value= ', type= ' text ', size=20)-%} <input type= "{{type}}" Name= "{{name}}" Value= "{{value|e}}" Size= "{{ size}}" >{%-Endmacro%}
In a namespace, a macro can then be called like a function:
<p>{{input (' username ')}}</p><p>{{input (' password ', type= ' password ')}}</p>
If a macro is defined in a different template, you need to first use the import
{% from ' base/macro/submit.macro ' Import test%}
3 parameters and variables
VarArgs If there are multiple macro of the number of arguments that are accepted, they are stored as the value of the list on the varargs variable. Kwargs is the same as varargs, but only for the keyword parameter. All unused keyword parameters are stored in this special variable. Caller if a macro is called by a call tag, the caller is stored in the variable as a callable macro. Macros can also expose certain internal details. The following macro object properties are available: The name of the name macro. {{Input.name}} will print input. Arguments a tuple of parameter names that a macro accepts. Defaults the default value of a tuple. Catch_kwargs True if the macro accepts additional keyword arguments (that is, access to special Kwargs variables). Catch_varargs True if the macro accepts additional positional parameters (that is, accesses a special varargs variable). Caller true if the macro accesses a special caller variable and is called by the call label.
4 Precautionsthe variables of the 4.1 macro can only be as follows: three
- Parameter variables
- Module variables
- Variables for Call
This is very different from other languages such as the C language macro. The variables you want the macro to refer to in the module are not possible.
4.2 and block relationship:
The emergence of macro, the essence is to solve the problem of duplicate code. As opposed to block, because the parameters in the module cannot be used, that is, the relationship with the block is mutually reinforcing. There is no problem of cross-covering.
The focus of the macro is to replace the simple repetitive nature of the code.
The center of the block is the repetition of the replacement of the frame code.
5 Reference Documents
Template Designer Documentation
Content Source: http://www.cnblogs.com/linuxhui/p/5804380.html
Summary of simple use of JINJA2 macros (macro)