[关闭]
@nalan90 2018-06-28T18:10:09.000000Z 字数 3000 阅读 710

专题十四 Jinja2常用操作

Python高效编程技巧实战


常用标签

There are a few kinds of delimiters. The default Jinja delimiters are configured as follows

  1. {% ... %} for Statements
  2. {{ ... }} for Expressions to print to the template output
  3. {# ... #} for Comments not included in the template output
  4. # ... ## for Line Statements

For
  1. ## Loop over each item in a sequence. For example, to display a list of users provided in a variable called users:
  2. <h1>Members</h1>
  3. <ul>
  4. {% for user in users %}
  5. <li>{{ user.username|e }}</li>
  6. {% endfor %}
  7. </ul>
  8. --------------------
  9. ## As variables in templates retain their object properties, it is possible to iterate over containers like dict:
  10. <dl>
  11. {% for key, value in my_dict.iteritems() %}
  12. <dt>{{ key|e }}</dt>
  13. <dd>{{ value|e }}</dd>
  14. {% endfor %}
  15. </dl>
  16. {% for row in rows %}
  17. <li class="{{ loop.cycle('odd', 'even') }}">{{ row }}</li>
  18. {% endfor %}
  19. {% for user in users if not user.hidden %}
  20. <li>{{ user.username|e }}</li>
  21. {% endfor %}
  22. <ul>
  23. {% for user in users %}
  24. <li>{{ user.username|e }}</li>
  25. {% else %}
  26. <li><em>no users found</em></li>
  27. {% endfor %}
  28. </ul>
  29. <ul class="sitemap">
  30. {%- for item in sitemap recursive %}
  31. <li><a href="{{ item.href|e }}">{{ item.title }}</a>
  32. {%- if item.children -%}
  33. <ul class="submenu">{{ loop(item.children) }}</ul>
  34. {%- endif %}</li>
  35. {%- endfor %}
  36. </ul>

If
  1. {% if users %}
  2. <ul>
  3. {% for user in users %}
  4. <li>{{ user.username|e }}</li>
  5. {% endfor %}
  6. </ul>
  7. {% endif %}
  8. ## For multiple branches, elif and else can be used like in Python. You can use more complex Expressions there, too:
  9. {% if kenny.sick %}
  10. Kenny is sick.
  11. {% elif kenny.dead %}
  12. You killed Kenny! You bastard!!!
  13. {% else %}
  14. Kenny looks okay --- so far
  15. {% endif %}

Assignments
  1. {% set iterated = false %}
  2. {% for item in seq %}
  3. {{ item }}
  4. {% set iterated = true %}
  5. {% endfor %}
  6. {% if not iterated %} did not iterate {% endif %}
  7. It is not possible with Jinja syntax to do this. Instead use alternative constructs like the loop else block or the special loop variable:
  8. {% for item in seq %}
  9. {{ item }}
  10. {% else %}
  11. did not iterate
  12. {% endfor %}

示例代码
  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. from jinja2 import Template
  4. def render_str(str, data):
  5. template = Template(str)
  6. val = template.render(data)
  7. return val
  8. with open('tpl01','r') as f:
  9. data = {
  10. 'name' : 'zhangshuang'
  11. }
  12. content = f.read()
  13. print render_str(content, data)
  14. ## tpl01
  15. My name is {{ name }}
  16. ## 运行结果
  17. (/anaconda) work:jinja2 ys$ python demo01.py
  18. My name is zhangshuang
  19. ----------------------------------------
  20. with open('tpl02','r') as f:
  21. data = {
  22. 'user' : {
  23. 'name': 'zhangshuang'
  24. },
  25. 'age' : 27
  26. }
  27. content = f.read()
  28. print render_str(content, data)
  29. ## tpl02
  30. name : {{ user.name }}
  31. age : {{ age }}
  32. ## 运行结果
  33. name : zhangshuang
  34. age : 27
  35. ----------------------------------------
  36. with open('tpl03','r') as f:
  37. data = {
  38. 'user_num_start' : 1000,
  39. 'user_list' : [
  40. {
  41. 'name' : 'zhangshuang',
  42. 'age' : 27
  43. },
  44. {
  45. 'name' : 'zhangsan',
  46. 'age' : 28
  47. },
  48. {
  49. 'name' : 'lisi',
  50. 'age' : 30
  51. },
  52. {
  53. 'name': 'unknow',
  54. 'age': 30
  55. }
  56. ]
  57. }
  58. content = f.read()
  59. print render_str(content, data)
  60. ## tpl03
  61. {%- for item in user_list %}
  62. <li>name : {{ item.name }},age : {{ item.age }}</li>
  63. {%- endfor %}
  64. {%- for item in user_list -%}
  65. {% set user_num = loop.index + user_num_start %}
  66. {%- if item.name != 'unknow' %}
  67. <li>name : {{ item.name }}, age : {{ item.age }}, user_num : {{ user_num }}</li>
  68. {%- endif %}
  69. {%- endfor -%}
  70. ## 运行结果
  71. <li>name : zhangshuang,age : 27</li>
  72. <li>name : zhangsan,age : 28</li>
  73. <li>name : lisi,age : 30</li>
  74. <li>name : unknow,age : 30</li>
  75. <li>name : zhangshuang, age : 27, user_num : 1001</li>
  76. <li>name : zhangsan, age : 28, user_num : 1002</li>
  77. <li>name : lisi, age : 30, user_num : 1003</li>
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注