Flask教程:http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-ii-templates

SOURCE

Python模板引擎http://jinja.pocoo.org/

相关文件

base.html

    
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        {% if title %}
            <title>{{ title }} microblog</title>
        {% else %}
            <title>Welcome to microblog</title>
        {% endif %}
    </head>
    <body>
        {% block content %}
        {% endblock %}
    </body>
    </html>
    
    

index.html

    
    {% extends "base.html" %}
    {% block content %}
        <h1>Hello, {{ user.nickname }}!!</h1>
        {% for post in posts %}
            <div><p>{{ post.author.nickname }} says: <b>{{ post.body }}</b></p></div>
        {% endfor %}
    {% endblock %}
    
    

views.py

    #coding:utf-8
    from app import app
    from flask import render_template
    
    
    @app.route('/')
    @app.route('/index')
    def index():
        user = {'nickname': 'Snow'} #fake user
        posts = [ #fake blog
            {
                'author' : {'nickname' : u'律香川'},
                'body' : u'蛋炒饭要么?'
            },
            {
                'author' : {'nickname' : u'郭大路'},
                'body' : u'你太像个女孩了!'
            }
        ]
        return render_template('index.html', title='Home', user=user, posts = posts)
    

模板应用

  1. 模板if语句
        
        <head>
        {% if title %}
        <title>{{ title }} - microblog</title>
        {% else %}
        <title>Welcome to microblog</title>
        {% endif %}
        </head>
        
        
  1. 模板循环语句
        
        {% for post in posts %}
            <div><p>{{ post.author.nickname }} says: <b>{{ post.body }}</b></p></div>
        {% endfor %}
        
        
  1. 模板继承
        
        {% extends "base.html" %}
        {% block content %}
            <h1>Hello, {{ user.nickname }}!!</h1>
            {% for post in posts %}
                <div><p>{{ post.author.nickname }} says: <b>{{ post.body }}</b></p></div>
            {% endfor %}
        {% endblock %}
        
        

模板设计

  1. 几种符号定义

    • {% … %} 语句
    • {{ … }} 输出表达式
    • {# … #} 注释
    • # … ## 行语句
  2. 变量

    {{ foo.bar }} {{ foo[‘bar’] }}

  3. 过滤器 {{ name|striptags|title }}

    内置过滤器http://jinja.pocoo.org/docs/dev/templates/#builtin-filters

  4. “测试器” {% if name is defined %}

    内置测试器http://jinja.pocoo.org/docs/dev/templates/#builtin-tests

  5. 转义 raw

  6. 继承

    • {% block title %}{% endblock %}
    • {% extends “base.html” %}
    • {{ super() }}
  7. 控制语句

    • for: {% for key, value in my_dict.iteritems() %}
    • if: {% for user in users %}
    • 宏:

        {% macro input(name, value='', type='text', size=20) -%}
        <input type="{{ type }}" name="{{ name }}" value="{{
        value|e }}" size="{{ size }}">
        {%- endmacro %}
      

      像函数一样调用:

        <p>{{ input('username') }}</p>
        <p>{{ input('password', type='password') }}</p>
      
  8. 包含

     {% include 'header.html' %}
     Body
     {% include 'footer.html' %}
    
  9. 计算 +,-,*,/…