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

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.

主站蜘蛛池模板: 诏安县| 利津县| 绥化市| 新宾| 得荣县| 伊川县| 边坝县| 唐海县| 资阳市| 金昌市| 博客| 长岛县| 溆浦县| 合川市| 柯坪县| 大同县| 巴里| 武安市| 都江堰市| 克什克腾旗| 嘉鱼县| 吴川市| 张家界市| 松潘县| 桑植县| 东台市| 霍林郭勒市| 白水县| 格尔木市| 三河市| 三河市| 桃源县| 泗水县| 调兵山市| 饶阳县| 壤塘县| 宿州市| 天津市| 沙雅县| 慈利县| 新余市|