flask- template folder

### template file folder: templates

1. The parameters can be passed
numbers, strings, lists, tuples, dictionaries, an object instance of the class

2 template file for internal use {{}} variable name


app.route @ ( '/ User')
DEF User ():
ARR = [ 'Monkey', 22 is]
Books = ( 'classic 1', 'famous 2', 'famous 3', 'famous. 4')
# return the render_template ( 'user.html', name = 'Monkey', age = 25)

# return render_template('user.html', arr=arr ,books=books)

print (locals ()) # dictionary, variable names: the key objects of the template files can be parms variable name call
return render_template ( 'user.html', parms = locals ())

 

Traverses internal receive parameters of the template file

{% For i in the list}%
.. executing statements
{% endfor%}


Receive parameters to determine the internal template file

IF parameter.age% {> 25%}
.. executing statements
{%}% the else
.. executing statements
{% endif%}


String filter ### parameter passing:

{{Variable name | upper}}

{{Variable name | lower}}

{{Variable name | title | trim}}

You can combine
filters filter Name Description
capitalize the first character of the first significant change to write, write the other characters changed a little
lower to convert the value into a small write
upper value is converted to uppercase to uppercase
title to the first value in the first character of each word becomes greatly write
the trim value across the space to remove


### template file reference style .css or .js or jquery.min.js

Static files should be placed in the folder: static
reference static files must be added / static /
src = "/ static / resource road route"

Macro definition (function) inside the template file ###
{% Macro color_ (STR, I)%}
{%} IF I ==. 1%
<Li style = "Color: Blue"> STR {{}} </ Li >
{%} elif I == 2%
<Li style = "Color: Red"> STR {{}} </ Li>
{%} the else%
<Li style = "Color: Green"> STR {{}} < / Li>
{% endif%}
{%}% endmacro

{{ color_(str,i) }}

 

For convenience, all of the macros can be placed inside macro.html file
to import, use
{% Import 'macro.html' AS%} m
{{m.show_li (the uname)}}

Navigation module enclosure ### / bottom
{% include 'xxx.html'%}

Guess you like

Origin www.cnblogs.com/chenlulu1122/p/11888884.html