天天看點

Flask模闆02

3.4 控制語句

常用的幾種控制語句:

模闆中的if控制語句

@app.route('/user')
def user():
    user = 'dongGe'
    return render_template('user.html',user=user)      
<html>
 <head>
     {% if user %}
        <title> hello {{user}} </title>
    {% else %}
         <title> welcome to flask </title>        
    {% endif %}
 </head>
 <body>
     <h1>hello world</h1>
 </body>
 </html>      

模闆中的for循環語句

@app.route('/loop')
 def loop():
    fruit = ['apple','orange','pear','grape']
    return render_template('loop.html',fruit=fruit)      
<html>
 <head>
     {% if user %}
        <title> hello {{user}} </title>
    {% else %}
         <title> welcome to flask </title>        
    {% endif %}
 </head>
 <body>
     <h1>hello world</h1>
    <ul>
        {% for index in fruit %}
            <li>{{ index }}</li>
        {% endfor %}
    </ul>
 </body>
 </html>      

3.5 宏、繼承、包含

類似于python中的函數,宏的作用就是在模闆中重複利用代碼,避免代碼備援。

Jinja2支援宏,還可以導入宏,需要在多處重複使用的模闆代碼片段可以寫入單獨的檔案,再包含在所有模闆中,以避免重複。

定義宏

{% macro input() %}
  <input type="text"
         name="username"
         value=""
         size="30"/>
{% endmacro %}      

調用宏

{{ input() }}      

定義帶參數的宏

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

調用宏,并傳遞參數

{{ input(value='name',type='password',size=40)}}      

把宏單獨抽取出來,封裝成html檔案,其它模闆中導入使用

檔案名可以自定義macro.html

{% macro function() %}

    <input type="text" name="username" placeholde="Username">
    <input type="password" name="password" placeholde="Password">
    <input type="submit">
{% endmacro %}      

在其它模闆檔案中先導入,再調用

{% import 'macro.html' as func %}
{% func.function() %}      

模闆繼承:

模闆繼承是為了重用模闆中的公共内容。一般Web開發中,繼承主要使用在網站的頂部菜單、底部。這些内容可以定義在父模闆中,子模闆直接繼承,而不需要重複書寫。

​{% block top %}``{% endblock %}​

​标簽定義的内容,相當于在父模闆中挖個坑,當子模闆繼承父模闆時,可以進行填充。

子模闆使用extends指令聲明這個模闆繼承自哪?父模闆中定義的塊在子模闆中被重新定義,在子模闆中調用父模闆的内容可以使用super()。

父模闆:base.html

{% block top %}
    頂部菜單
  {% endblock top %}

  {% block content %}
  {% endblock content %}

  {% block bottom %}
    底部
  {% endblock bottom %}      

子模闆:

{% extends 'base.html' %}
  {% block content %}
   需要填充的内容
  {% endblock content %}      
  • 模闆繼承使用時注意點:
  • 不支援多繼承。
  • 為了便于閱讀,在子模闆中使用extends時,盡量寫在模闆的第一行。
  • 不能在一個模闆檔案中定義多個相同名字的block标簽。
  • 當在頁面中使用多個block标簽時,建議給結束标簽起個名字,當多個block嵌套時,閱讀性更好。

包含(Include)

Jinja2模闆中,除了宏和繼承,還支援一種代碼重用的功能,叫包含(Include)。它的功能是将另一個模闆整個加載到目前模闆中,并直接渲染。

示例:

include的使用

{\% include 'hello.html' %}

包含在使用時,如果包含的模闆檔案不存在時,程式會抛出TemplateNotFound異常,可以加上ignore missing關鍵字。如果包含的模闆檔案不存在,會忽略這條include語句。

示例:

include的使用加上關鍵字ignore missing

{\% include 'hello.html' ignore missing %}

  • 宏、繼承、包含:
  • 宏(Macro)、繼承(Block)、包含(include)均能實作代碼的複用。
  • 繼承(Block)的本質是代碼替換,一般用來實作多個頁面中重複不變的區域。
  • 宏(Macro)的功能類似函數,可以傳入參數,需要定義、調用。
  • 包含(include)是直接将目标模闆檔案整個渲染出來。

3.6 Flask中的特殊變量和方法:

在Flask中,有一些特殊的變量和方法是可以在模闆檔案中直接通路的。

config 對象:

config 對象就是Flask的config對象,也就是 app.config 對象。

{{ config.SQLALCHEMY_DATABASE_URI }}      

request 對象:

就是 Flask 中表示目前請求的 request 對象,request對象中儲存了一次HTTP請求的一切資訊。

request常用的屬性如下:

屬性 說明 類型
data 記錄請求的資料,并轉換為字元串 *
form 記錄請求中的表單資料 MultiDict
args 記錄請求中的查詢參數 MultiDict
cookies 記錄請求中的cookie資訊 Dict
headers 記錄請求中的封包頭 EnvironHeaders
method 記錄請求使用的HTTP方法 GET/POST
url 記錄請求的URL位址 string
files 記錄請求上傳的檔案 *
{{ request.url }}      

url_for 方法:

url_for() 會傳回傳入的路由函數對應的URL,所謂路由函數就是被 app.route() 路由裝飾器裝飾的函數。如果我們定義的路由函數是帶有參數的,則可以将這些參數作為命名參數傳入。

{{ url_for('index') }}

{{ url_for('post', post_id=1024) }}      
{% for message in get_flashed_messages() %}
    {{ message }}
{% endfor %}