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

  • Flask By Example
  • Gareth Dwyer
  • 236字
  • 2021-07-09 20:06:54

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.

主站蜘蛛池模板: 正宁县| 襄城县| 太康县| 沂水县| 朝阳市| 江陵县| 张家口市| 常山县| 河池市| 阿拉尔市| 浦江县| 利津县| 湄潭县| 明光市| 南宫市| 正安县| 抚宁县| 修水县| 哈密市| 和硕县| 应城市| 苏尼特右旗| 鸡东县| 勐海县| 琼海市| 东明县| 武胜县| 佳木斯市| 澄迈县| 清远市| 南皮县| 封开县| 盱眙县| 广灵县| 吉林省| 阿勒泰市| 海南省| 普洱| 朝阳区| 慈利县| 贺州市|