美文网首页
2.4 jinja2控制语句

2.4 jinja2控制语句

作者: yungege | 来源:发表于2019-08-19 11:39 被阅读0次

    控制语句

    所有的控制语句都是放在{% ... %}中,并且有一个语句{% endxxx %}来进行结束,Jinja中常用的控制语句有if/for..in..,现对他们进行讲解:

    if

    1. if:if语句和python中的类似,可以使用>,<,<=,>=,==,!=来进行判断,也可以通过and,or,not,()来进行逻辑合并操作,以下看例子:

      {% if kenny.sick %}
         Kenny is sick.
      {% elif kenny.dead %}
       You killed Kenny!  You bastard!!!
      {% else %}
       Kenny looks okay --- so far
      {% endif %}
      {% if username == 'zhiliao' %}
      <p>知了课堂</p>
      {% else %}
      <p>我不是知了课堂</p>
      {% endif %}
      
      {% if age >= 18 %}
      <p>你可以进入网吧了</p>
      {% else %}
      <p>未成年人禁止进入网吧</p>
      {% endif %}
      

    for

    1. for...in...for循环可以遍历任何一个序列包括列表、字典、元组。并且可以进行反向遍历,以下将用几个例子进行解释:

      • 普通的遍历:

           <ul>
           {% for user in users %}
           <li>{{ user.username|e }}</li>
           {% endfor %}
           </ul>
        
      • 遍历字典:

           <dl>
           {% for key, value in my_dict.iteritems() %}
           <dt>{{ key|e }}</dt>
           <dd>{{ value|e }}</dd>
           {% endfor %}
           </dl>
        
      • 如果序列中没有值的时候,进入else

           <ul>
           {% for user in users %}
           <li>{{ user.username|e }}</li>
           {% else %}
           <li><em>no users found</em></li>
           {% endfor %}
           </ul>
        

      并且Jinja中的for循环还包含以下变量,可以用来获取当前的遍历状态:

      变量 描述
      loop.index 当前迭代的索引(从1开始)
      loop.index0 当前迭代的索引(从0开始)
      loop.first 是否是第一次迭代,返回True或False
      loop.last 是否是最后一次迭代,返回True或False
      loop.length 序列的长度

      另外,不可以使用continuebreak表达式来控制循环的执行。

      from flask import Flask,render_template
      
      app = Flask(__name__)
      app.config['TEMPLATES_AUTO_RELOAD'] = True
      
      @app.route('/')
      def index():
          context = {
              'users':['zhiliao1','zhiliao2','zhiliao3'],
              'person': {
                  'username': 'zhiliao',
                  'age': 18,
                  'country': 'china'
              },
              'books':[
                  {
                      'name': '三国演义',
                      'author':'罗贯中',
                      'price': 110
                  },{
                      'name': '西游记',
                      'author':'吴承恩',
                      'price': 109
                  },{
                      'name': '红楼梦',
                      'author':'曹雪芹',
                      'price': 120
                  },{
                      'name': '水浒传',
                      'author':'施耐庵',
                      'price': 119
                  }
              ]
          }
          return render_template('index.html',**context)
      
      
      if __name__ == '__main__':
          app.run(debug=True)
      
      <!DOCTYPE html>
      <html lang="en">
      <head>
          <meta charset="UTF-8">
          <title>知了for循环</title>
      </head>
      <body>
          <ul>
              {% for user in users|reverse %}
                  <li>{{ user }}</li>
              {% else %}
                  <li>沒有任何值</li>
              {% endfor %}
          </ul>
      
          <table>
              <thead>
                  <tr>
                      <th>用户名</th>
                      <th>年龄</th>
                      <th>国家</th>
                  </tr>
              </thead>
              <tbody>
                  <tr>
                      {% for key in person.keys() %}
                          <td>{{ key }}</td>
                      {% endfor %}
                  </tr>
              </tbody>
          </table>
      
          <table>
              <thead>
                  <tr>
                      <th>序号</th>
                      <th>书名</th>
                      <th>作者</th>
                      <th>价格</th>
                      <th>总数</th>
                  </tr>
              </thead>
              <tbody>
                  {% for book in books %}
                      {% if loop.first %}
                          <tr style="background: red;">
                      {% elif loop.last %}
                          <tr style="background: pink;">
                      {% else %}
                          <tr>
                      {% endif %}
                          <td>{{ loop.index0 }}</td>
                          <td>{{ book.name }}</td>
                          <td>{{ book.author }}</td>
                          <td>{{ book.price }}</td>
                          <td>{{ loop.length }}</td>
                      </tr>
                  {% endfor %}
              </tbody>
          </table>
      
          <table border="1">
              <tbody>
                  {% for x in range(1,10) %}
                      <tr>
                          {% for y in range(1,10) if y <= x %}
                              <td>{{ y }}*{{ x }}={{ x*y }}</td>
                          {% endfor %}
                      </tr>
                  {% endfor %}
              </tbody>
          </table>
      </body>
      </html>
      

    相关文章

      网友评论

          本文标题:2.4 jinja2控制语句

          本文链接:https://www.haomeiwen.com/subject/oplssctx.html