python-Flask(jinja2)语法:过滤器

2018-03-30  本文已影响0人  SmallPot_Yang

default过滤器

[TOC]

default过滤器

{{ avatar|default('xxx') }}
<body>
    <p>过滤器模板文件</p>
    <img src="{{ avatar | default('http://noavatar.csdn.net/B/3/F/3_lighting_miaoxingren.jpg') }}">
</body>
@app.route('/')
def index():
    return render_template('index.html') # 后端不上传图片,前端将加载默认的图片

length过滤器

获取列表、字符串、字典、元组等长度。例如用来显示文字评论的总数

 <body>
    <p>评论数:({{ comments|length }})</p>
    <ul>
        {% for comment in comments %}
            <li>
                <a href="#">{{ comment.user }}</a>
                <p href="#">{{ comment.content }}</p>
            </li>
        {% endfor %}
    </ul>
</body>

@app.route('/')
def index():
    # 定义一个评论列表
    comments = [
        {
            'user' : '站长',
            'content' : '我觉得可以'
        },
        {
            'user' : '你猜',
            'content' : '我觉得不行'
        },
        {
            'user' : '杰克',
            'content' : '你有Freestyle吗?'
        }
    ]
    return render_template('index.html',comments=comments)

常用的过滤器

default过滤器

[TOC]

default过滤器

{{ avatar|default('xxx') }}
<body>
    <p>过滤器模板文件</p>
    <img src="{{ avatar | default('http://noavatar.csdn.net/B/3/F/3_lighting_miaoxingren.jpg') }}">
</body>
@app.route('/')
def index():
    return render_template('index.html') # 后端不上传图片,前端将加载默认的图片

length过滤器

获取列表、字符串、字典、元组等长度。例如用来显示文字评论的总数

 <body>
    <p>评论数:({{ comments|length }})</p>
    <ul>
        {% for comment in comments %}
            <li>
                <a href="#">{{ comment.user }}</a>
                <p href="#">{{ comment.content }}</p>
            </li>
        {% endfor %}
    </ul>
</body>

@app.route('/')
def index():
    # 定义一个评论列表
    comments = [
        {
            'user' : '站长',
            'content' : '我觉得可以'
        },
        {
            'user' : '你猜',
            'content' : '我觉得不行'
        },
        {
            'user' : '杰克',
            'content' : '你有Freestyle吗?'
        }
    ]
    return render_template('index.html',comments=comments)

常用的过滤器

上一篇 下一篇

猜你喜欢

热点阅读