flash 烟花模板_Flask模板引擎中的For循环

flash 烟花模板

Flask comes with a Jinja templating language. The template essentially contains variables as well as some programming logic, which when evaluated are rendered into HTML with actual values. The variables and/or logic are placed between tags or delimiters.

Flask带有Jinja模板语言 。 该模板实质上包含变量以及一些编程逻辑,这些逻辑在进行评估时会以实际值呈现为HTML。 变量和/或逻辑放置在标签或定界符之间。

Jinja templates are HTML files which by flask conventions are placed in the templates folder in the Flask project.

Jinja模板是HTML文件,根据烧瓶惯例,它们位于Flask项目的template文件夹中。

Having a loop of the list of users or any list as a matter is a very obvious requirement in a web application. It would be very naïve and unmanageable to add a new page for every new item in the list. Here is where the for-loop in the jinja templates are useful.

Web应用程序中非常明显的要求是拥有用户列表或任何列表的循环 。 为列表中的每个新项目添加一个新页面将非常幼稚且难以管理。 这是Jinja模板中的for循环有用的地方。

Consider the following example,

考虑以下示例,

from flask import Flask, render_template

app = Flask(__name__)

@app.route("/jinja")
def jinja_test():
    return render_template('include_help.html', my_string="Include Help!", my_list=[0,1,2,3,4,5])
  
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5005)

In the above example, when in the browser the user navigates to http://localhost:5005/jinja the rendered web page will sequentially display the list number.

在上面的示例中,当用户在浏览器中导航到http:// localhost:5005 / jinja时 ,呈现的网页将顺序显示列表号。

The list could as simple as a list of numbers or users or profiles to make it complex.

该列表可能像数字列表,用户列表或个人资料列表一样简单,从而使其变得复杂。

The 'include_help.html' is implemented as

“ include_help.html”的实现方式为

<html>

<head>
    <title>Include help Template Example</title>
</head>

<body>
    <p>{{my_string}}</p>
    <p>Your lucky number is ;) : {{my_list[3]}}</p>
    <p>Loop through the list:</p>
    <ul>
        {% for n in my_list %}
        <li>{{n}}</li>
        {% endfor %}
    </ul>
</body>

</html>

On rendering of the template, the my_string and my_list is replaced with the actual values sent from the backend code. The my_list is then iterated using a for loop. The for loop starts with a for and ends with endfor inside the {%%} tag.

呈现模板时, my_string和my_list替换为从后端代码发送的实际值。 然后使用for循环迭代my_list 。 for循环以{%%}标记内的for开头,以endfor结尾。

On running the above code, the html rendered is,

运行以上代码后,呈现的html是,

for loop in Python flask

翻译自: https://www.includehelp.com/python/for-loop-in-flask-template-engine.aspx

flash 烟花模板

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值