python编程基础:快速微服务框架指南:flask:3: 活用Jinja2 - Go语言中文社区

python编程基础:快速微服务框架指南:flask:3: 活用Jinja2


这里写图片描述
在上一篇文章中我们介绍了在flask中如何使用页面模版(template),也提到过这种方式是基于Jinja2,在这篇文章中进一步进行使用的介绍。

概要信息

项目 说明
官方网站 https://www.palletsprojects.com/p/flask/
开源/闭源 开源
License类别 BSD License
代码管理地址 https://github.com/pallets/flask
开发语言 Python
支持平台 鉴于python的跨平台特性,可运行于等多种操作系统
当前版本 1.0.2 (2018/05/02)

事前准备:flask

liumiaocn:flask liumiao$ which flask
/usr/local/bin/flask
liumiaocn:flask liumiao$ flask --version
Flask 1.0.2
Python 2.7.10 (default, Jul 15 2017, 17:16:57) 
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)]
liumiaocn:flask liumiao$ 

代码示例:数据的传递

就像angular的插值表达式在模版中的作用一样,在flask中也可以一样使用,如果不熟悉angular的插值表达式的话也不要紧,看完下面的例子,基本上就会有一个大致的印象。

代码示例:插值表达式

liumiaocn:flask liumiao$ cat flask_3.py 
#!/usr/bin/python

from flask import Flask
from flask import render_template

app = Flask(__name__)

greeting_message = "Hello Message using Jinja"

@app.route("/")
def hello():
    return render_template("templatetest.html",message=greeting_message)

if __name__ == "__main__":
    app.debug=True
    app.run(host='0.0.0.0',port=7000)
liumiaocn:flask liumiao$

模版文件

liumiaocn:flask liumiao$ cat templates/templatetest.html 
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>Hello Template</title>
</head>

<body>
  <h1>{{ message }}</h1>
</body>

</html>
liumiaocn:flask liumiao$

代码解析:代码只需要注意两点,在python的代码中,render_template(“templatetest.html”,message=greeting_message),通过这样的一行语句将变量与之进行关联,这样在模版文件中则可以直接使用插值表达式直接引用了。插值表达式的引用方式为{{ 变量名称 }}。

执行&确认

liumiaocn:flask liumiao$ ./flask_3.py 
 * Serving Flask app "flask_3" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://0.0.0.0:7000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 131-533-062

结果页面确认
这里写图片描述

代码示例:循环

liumiaocn:flask liumiao$ cat flask_3.py 
#!/usr/bin/python

from flask import Flask
from flask import render_template

app = Flask(__name__)

greeting_messages=["Hello, Greetings from World",
                   "Hello, Grettings from Jinja",
                   "Hello, Greetings from liumiao"]

@app.route("/")
def hello():
    return render_template("templatetest.html",messages=greeting_messages)

if __name__ == "__main__":
    app.debug=True
    app.run(host='0.0.0.0',port=7000)
liumiaocn:flask liumiao$ 

模版代码:

liumiaocn:flask liumiao$ cat templates/templatetest.html 
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>Hello Template</title>
</head>

<body>
        {% for message in messages %}
    <h1>{{ message }}</h1>
        {% endfor %}
</body>

</html>
liumiaocn:flask liumiao$

代码解析:唯一需要注意的是引用的时候的格式,{% for … in … %}以及其结束标志

执行&确认

liumiaocn:flask liumiao$ ./flask_3.py 
 * Serving Flask app "flask_3" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://0.0.0.0:7000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 131-533-062

结果确认
这里写图片描述

代码示例:逻辑控制

显示循环信息的奇数部分,可以通过if判断进行确认,这里仅仅需要修改模版部分的代码即可

liumiaocn:flask liumiao$ cat templates/templatetest.html 
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>Hello Template</title>
</head>

<body>
        {% for message in messages %}
        {% if not loop.index is divisibleby 2 %}
    <h1>{{ message }}</h1>
        {% endif %}
        {% endfor %}
</body>

</html>
liumiaocn:flask liumiao$

执行&确认

liumiaocn:flask liumiao$ ./flask_3.py 
 * Serving Flask app "flask_3" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://0.0.0.0:7000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 131-533-062

结果确认
这里写图片描述

总结

通过使用Jinja模版的方式,在flask中可以非常容易地进行数据的传递以及循环和条件控制等操作,虽然Jinja的模版强大的功能不是这几个简单例子可以展示的,但是至少可以让我们进行管中窥豹,见其一斑。

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/liumiaocn/article/details/80726659
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2019-08-27 00:12:30
  • 阅读 ( 1633 )
  • 分类:Go Web框架

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢