Jinja2 learning

Template: well-known template engine: jinja2 (flask default template engine) Mako
Template Template path:
1. When rendering a template, it will look for the default template file from the templates directory at the root of
2. You can also customize the template path, Flask class constructor defined template path parameter, it is possible to specify at the time of Flask template_folder initialization parameters.

Template variable parameter passing:
a single variable can be placed directly render_templates () parameters in
a plurality of variables, the parameters may be a variable value in the dictionary,
the render_template () function name can be added to the dictionary **

The front end using a variable parameter:
{{}} is used to store variables
{% to execute the function or logic code}%
shortcut: the Tab
start tag end tag
jinja for loop:
{% for% foo in List}
{IF Loop%. %} First
...
{% endif%}
{%} endfor%
other loop method:
loop.last loop.index0 (current iteration index, starting from 0) loop.index, loop.length (sequence length)
Jinja2 template filter:
action: the parameters passed back reception data processing
using: via conduit variable name symbols {{|}} filter name

commonly used filter:
default filter: variable name {{1 | default ( 'xxx . ', boolean = False)} } 2 may be used instead of or default {{variable name or ""}}

escape filter: default Jinja2 automatically escapes into the string rather than html tags
using a filter: to prevent the escape block area to prevent escape:
{%} {OFF autoescape%%%} endautoescape
univariate prevent escape:
{variable name% | safe%}
a string escape:
escape

the format filters:
{{ "My name is% s" | format ( "hello ")}}

Other filters:
First Last length the Join int Lower Replace (Old, new new) TRUNCATE (length = 30) taken striptags string wordcount () calculates the word a number of

custom filters:
a rear end defined function: the custom function, which is then registered to the template jinja2
@ app.template_filter ( 'my_func')
DEF FUNC (value):
value = xxx.x
return value


Macro:
Concept: template-like macros and function in the python, the parameters can be passed, but the code fragment not have a return value may be frequently used into a macro, and then extracted as a variable parameter value is not fixed

use: parameter to a default value
1. html asking price may be present in the
2 tml may be placed in a separate file, import it (introducing macro file path {% from import file name macro macro as%.} the default is to look in the templates directory)
is automatically variable transmission values:
introducing the macro when the macro is introduced For the variable value html file automatically passed to the macro, the import was added with context

definitions macros:
{% macro INPUT ( name, value = '', type = '{{value}}'%}
<INPUT type = "type" name = "{{name}}" value = "{{value}}">
{% endmacro%}
use macro:
<P> {{INPUT ( 'username')}} </ P>
<P> {{INPUT ( 'password', type = 'text')}} </ P>

Jinja2 the tags include:
1. the tag is equivalent to directly copy and paste the template code to the current location
2. "include" tag, if you want to use a variable parent template, the direct use of it, without context
3.

Guess you like

Origin www.cnblogs.com/szj666/p/11479370.html