flask_jinja2

Jinja2 模板过滤器:

    "books":["python", "jiava"],

<p>{{ books|first}}</p>
<p>{{ books|last}}</p>
<p>{{ books|length}}</p>
<p>{{ books|first|length}}</p>
<p>{{ books.1}}</p>
<p>{{ name|upper}}</p>
<p>{{ name|replace('hahha','hhhhhhhhh')}}</p>
<p>{{ name|truncate(length=5)}}</p>
<p>{{ es|striptags}}</p>
<p>{{ name|my_cate}}</p>
<p>文章发布时间:{{now_time|handle_time}}</p>

宏:模板中的宏跟python中的函数类似,可以传递参数,但是不能有返回值,可以将一些经常用到的代码片段放到宏中

{% macro input(name, value='', type='text') %}
    <input type="{{ type }}", name="{{ name }}",       value="{{ value }}">
{% endmacro %}
<table>
    <tr>
        <td>用户名:</td>
        <td>{{input('username')}}</td>
    </tr>
    <tr>
        <td>密码:</td>
        <td>{{input('password', type='password')}}</td>
    </tr>
</table>

导入宏的代码:
{% import 'marco.html' as macro %}

{% import 'marco.html' as macro with context %}
{% from "marco.html" import input %}
{% from "marco.html" import input %}

在模板中如何调用宏:

{{macro.input('username')}}
{{ input('password', type="password")}}

 
Include语句:可以把一个模板引入另一个模板中,类似于把一个模板的代码copy到另一个模板的指定位置

{% set username='asdfsf' %}

{% include 'commit/header.html' %}
    <p>{{ username}}</p>
{% include 'commit/footer.html' %}

{% with %}
    {% set username='yyyy' %}
    <p>{{username}}</p>
{% endwith %}

posted @ 2021-01-23 14:47  枫叶少年  阅读(70)  评论(0)    收藏  举报