官术网_书友最值得收藏!

Introduction

In Flask, we can write a complete web application without the need of any third-party templating engine. For example, have a look at the following code; this is a simple Hello World application with a bit of HTML styling included:

from flask import Flask
app = Flask(__name__)

@app.route('/')
@app.route('/hello')
@app.route('/hello/<user>')
def hello_world(user=None):
    user = user or 'Shalabh'
    return '''
<html>
    <head>
      <title>Flask Framework Cookbook</title>

    </head>
      <body>
        <h1>Hello %s!</h1>
        <p>Welcome to the world of Flask!</p>
      </body>
</html>''' % user

if __name__ == '__main__':
    app.run()

Is the preceding pattern of writing the application feasible in the case of large applications that involve thousands of lines of HTML, JS, and CSS code? Obviously not!

Here, templating saves us because we can structure our view code by keeping our templates separate. Flask provides default support for Jinja2, although we can use any templating engine as suited. Furthermore, Jinja2 provides many additional features that make our templates very powerful and modular.

主站蜘蛛池模板: 普定县| 长沙市| 扶绥县| 麦盖提县| 丹江口市| 白河县| 东台市| 百色市| 阿克陶县| 浦东新区| 伽师县| 晋州市| 阜南县| 安康市| 五台县| 宁津县| 宁远县| 陇川县| 栾城县| 淮南市| 荆州市| 元阳县| 石景山区| 三河市| 兰州市| 武胜县| 黄陵县| 昭觉县| 长岛县| 大庆市| 北票市| 资兴市| 台山市| 台州市| 门头沟区| 佛冈县| 定远县| 故城县| 湘阴县| 泊头市| 安岳县|