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

Introducing Jinja

Jinja is a Python template engine. It allows us to easily define dynamic blocks of HTML which are populated by Python. HTML templates are useful even for static websites which have multiple pages. Usually, there are some common elements, such as headers and footers, on every page. Although it is possible to maintain each page inpidually for static websites, this requires that a single change be made in multiple places if the change is made to a shared section. Flask was built on top of Jinja, so although it is possible to use Jinja without Flask, Jinja is still an inherent part of Flask, and Flask provides several methods to work directly with Jinja. Generally, Flask assumes nothing about the structure of your application except what you tell it, and prefers providing functionality through optional plugins. Jinja is somewhat of an exception to this. Flask gives you Jinja by default, and assumes that you store all your Jinja templates in a subdirectory of your application named templates.

Once we've created templates, we'll make calls from our Flask app to render these templates. Rendering involves parsing the Jinja code, inserting any dynamic data, and creating pure HTML to be returned to a user's browser. All of this is done behind the scenes though, so it can get a bit confusing as to what is being done where. We'll take things one step at a time.

主站蜘蛛池模板: 德昌县| 建昌县| 孟津县| 武平县| 滨州市| 丹江口市| 抚州市| 临沭县| 江源县| 无极县| 呼玛县| 泰和县| 云阳县| 南华县| 河池市| 武宁县| 榆树市| 广河县| 武胜县| 仪征市| 武义县| 成安县| 额济纳旗| 兴仁县| 库伦旗| 西平县| 舟曲县| 赤城县| 郓城县| 延安市| 松溪县| 景泰县| 大竹县| 玛纳斯县| 斗六市| 连江县| 陇南市| 海宁市| 潮州市| 定边县| 乡宁县|