flask的模板语言

目录


Flask的模板语言是按Jinja2模板语言的标准,基于jinja2做了一点点的封装

1.使用STUDENT字典传递到前端

后端:

@app.route("/student")
def index():

    return render_template("student.html", student=STUDENT)

前端:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>student information manage</title>
</head>
<body>
<div>{{ student }}</div>
<table border="1px">
           <tr>
               <td>{{ student.name }}</td>
               <td>{{ student["age"] }}</td>
               <td>{{ student.get("gender") }}</td>
           </tr>
</table>
</body>
</html>

2. STUDENT_LIST 列表传入前端Jinja2 模板的操作

后端:

@app.route("/student_list")
def student_list():

    return render_template("student_list.html", student=STUDENT_LIST)

前端:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>student information manage</title>
</head>
<body>
<div>{{ student }}</div>
<table border="1xp">
    {% for foo in student %}
        <tr>
            <td>{{ foo }}</td>
            <td>{{ foo.name }}</td>
            <td>{{ foo.get("age") }}</td>
            <td>{{ foo["gender"] }}</td>
        </tr>
    {% endfor %}
</table>
</body>
</html>

3.STUDENT_DICT 大字典传入前端 Jinja2 模板

后端:

@app.route("/student_dict")
def student_dict():
    return render_template("student_dict.html", student=STUDENT_DICT)

前端:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>student information manage</title>
</head>
<body>
<table>
    {% for foo in student %}
        <tr>
            <td>{{ foo }}</td>
            <td>{{ student.get(foo).name }}</td>
            <td>{{ student[foo].get("age") }}</td>
            <td>{{ student[foo]["gender"] }}</td>
        </tr>
    {% endfor %}
</table>
</body>
</html>

在遍历字典的时候,foo 其实是相当于拿出了字典中的Key

4.结合所有的字符串儿全部专递前端Jinja2 模板

后端:

@app.route("/allstudent")
def all_student():
    return render_template("all_student.html", student=STUDENT ,
                           student_list = STUDENT_LIST,
                           student_dict= STUDENT_DICT)

前端:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>student information manage</title>
</head>
<body>
<div>{{ student }}</div>
<table border="1px">
    <tr>
        <td>{{ student.name }}</td>
        <td>{{ student["age"] }}</td>
        <td>{{ student.get("gender") }}</td>
    </tr>
</table>
<hr>
<div>{{ student_list }}</div>
<table border="1xp">
    {% for foo in student_list %}
        <tr>
            <td>{{ foo }}</td>
            <td>{{ foo.name }}</td>
            <td>{{ foo.get("age") }}</td>
            <td>{{ foo["gender"] }}</td>
        </tr>
    {% endfor %}
</table>
<hr>
<div>{{ student_dict }}</div>
<table border="1xp">
    {% for foo in student_dict %}
        <tr>
            <td>{{ foo }}</td>
            <td>{{ student_dict.get(foo).name }}</td>
            <td>{{ student_dict[foo].get("age") }}</td>
            <td>{{ student_dict[foo]["gender"] }}</td>
        </tr>
    {% endfor %}
</table>
</body>
</html>
View Code

render_template中可以传递多个关键字。当前端定义了jinja2的变量,而后端有什么都没传,会报错。而后端传个空字典或关键字参数就能解决。

5.利用 **{}字典的方式传递参数

后端:

@app.route("/allstudent")
def all_student():
    return render_template("all_student.html", **{"student":STUDENT ,
                           "student_list" : STUDENT_LIST,
                           "student_dict": STUDENT_DICT})

6. Jinja2 的高阶用法

6.1. safe :

后端代码:
from flask import Flask
from flask import render_template

app = Flask(__name__)

@app.route("/")
def index():
    tag = "<input type='text' name='user' value='aa'>"
    return render_template("index.html",tag=tag)

app.run("0.0.0.0",5000)
前端代码:
{{tag|safe}}

6.2Markup

后端代码:
from flask import Flask
from flask import render_template
from flask import Markup  # 导入 flask 中的 Markup 模块

app = Flask(__name__)

@app.route("/")
def index():
    tag = "<input type='text' name='user' value=’aa’>"
    markup_tag = Markup(tag)  # Markup帮助咱们在HTML的标签上做了一层封装,让Jinja2模板语言知道这是一个安全的HTML标签
    return render_template("index.html", tag=markup_tag)

app.run("0.0.0.0", 5000, debug=True)
前端代码:
{{tag}}

6.3从后端传函数到前端

后端代码
from flask import Flask
from flask import render_template
from flask import Markup  # 导入 flask 中的 Markup 模块

app = Flask(__name__)

#定义一个函数,把它传递给前端
def a_b_sum(a,b):
    return a+b

@app.route("/")
def index():
    return render_template("index.html", tag=a_b_sum)

app.run("0.0.0.0", 5000, debug=True)
前端代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    {{ tag }}
    <br>
    {{ tag(99,1) }}
</body>
</html>

6.4定义全局函数,无需后端传递给前端,Jinja2直接就可以执行的函数

后端代码:
from flask import Flask
from flask import render_template
from flask import Markup  # 导入 flask 中的 Markup 模块

app = Flask(__name__)

@app.template_global()  # 定义全局模板函数
def a_b_sum(a, b):
    return a + b

@app.template_filter()  # 定义全局模板函数
def a_b_c_sum(a, b, c):
    return a + b + c

@app.route("/")
def index():
    return render_template("index.html", tag="")

app.run("0.0.0.0", 5000, debug=True)
前端代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    {{ a_b_sum(99,1) }}
    <br>
    {{ a_b_sum(99,1) | a_b_c_sum(197,2) }}
    <!—将管道符前的函数的结果当做第二个函数的一个参数传入 -->
</body>
</html>

7.Jinja2模板复用 block

如果我们前端页面有大量重复页面,没必要每次都写,可以使用模板复用的方式复用模板

前端代码:

index.html 文件中的内容
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>Welcome OldboyEDU</h1>
    <h2>下面的内容是不一样的</h2>
    {% block content %}
 
    {% endblock %}
    <h2>上面的内容是不一样的,但是下面的内容是一样的</h2>
    <h1>OldboyEDU is Good</h1>
</body>
</html>
login.html 文件中的内容
{% extends "index.html" %}
{% block content %}
    <form>
        用户名:<input type="text" name="user">
        密码:<input type="text" name="pwd">
    </form>
{% endblock %}
home.html 文件中的内容
{% extends "index.html" %}
{% block content %}
    <h1>student information manage</h1>
{% endblock %}

后端代码:

from flask import Flask
from flask import render_template

app = Flask(__name__)

@app.route("/login")
def login():
    return render_template("login.html")

@app.route("/home")
def home():
return render_template("home.html")

app.run("0.0.0.0", 5000, debug=True)

8.Jinja2模板语言的模块引用 include

login.html 文件中的内容:
<form>
    用户名:<input type="text" name="user">
    密码:<input type="text" name="pwd">
</form>
index.html 文件中的内容
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>student information manage</h1>
    <h2>下面的内容是不一样的</h2>
    {% include "login.html" %}
    <h2>上面的内容是不一样的,但是下面的内容是一样的</h2>
</body>
</html>

后端代码:

from flask import Flask
from flask import render_template

app = Flask(__name__)

@app.route("/")
def index():
    return render_template("index.html")

app.run("0.0.0.0", 5000, debug=True)

9. Jinja2模板语言中的宏定义

前端代码:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
{% macro type_text(name,type) %}
    <input type="{{ type }}" name="{{ name }}" value="{{ name }}">
{% endmacro %}
<h2>在下方是使用宏来生成input标签</h2>
{{ type_text("one","text") }}
{{ type_text("two","text") }}
</body>
</html>

 

posted @ 2018-08-20 20:02  桥前石头  阅读(1327)  评论(0编辑  收藏  举报