flask(一)jinja模板

1、flask的默认模板是jinja ,模板默认目录是你开发的应用目录下的templates ,例:app/templates

2、导入模板传递函数包 :from flask import render_template

       render——template(arguments)主要参数:第一个参数是模板名,之后的参数是模板中需要填充的动态变量参数。

     例:

render_template("home.html",
                title=first_article.get("title"),
                published=first_article.get("published"),
                summary=first_article.get("summary"))

      在模板中用双大括号表示变量的内容:

     例:

<html>
    <head>
    <title>Headlines</title>
    </head>
    <body>
         <h1>Headlines</h1>
         <b>{{title}}</b><br />
         <i>{{published}}</i><br />
         <p>{{summary}}</p>
    </body>
</html>

3、模板高级用法:

  • 使用模板对象:    Python内置的数据结构和对象(包括变量、对象、列表、字典等等)都能被jinja识别和处理 ,处理方式和在Python中一致。如上面的例子可以简化为: 
    render_template("home.html", article=first_article)
    <b>{{article.title}}</b><br />
    <i>{{article.published</i><br />
    <p>{{article.summary}}</p>

     

  • 使用逻辑判断:模板中可以使用for循环、if...else...等逻辑判断功能,用{%  %}来表示逻辑控制功能如:
    <body>
        <h1>Headlines</h1>
        {% for article in articles %}
             <b>{{article.title}}</b><br />
             <i>{{article.published}}</i><br />
             <p>{{article.summary}}</p>
             <hr />
        {% endfor %}
    </body

     

  • 添加超链接:
    <b><a href="{{article.link}}">{{article.title}}</a></b><br />

     

 

转载于:https://my.oschina.net/u/2440318/blog/782689

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值