Jinja2的一些使用摘要
Jinja2作为flask的默认页面模板,感觉蛮强大的,可适用于各种模板类相关的项目
特别是模板继承,写页面很舒服
资源
文档 http://jinja.pocoo.org/docs/
安装
| 12 | sudo easy_install jinja2sudo pip install jinja2 |
终端版helloworld
| 1234 | >>> from jinja2 import Template>>> template = Template(\’Hello {{ name }}!\’)>>> template.render(name=\’World\’)u\’Hello World!\’ |
{%和{{
| 1234567 | id=\”navigation\”> {% for item in navigation %} href=\”{{ item.href }}\”>{{ item.caption }} {% endfor %} My Webpage {{ a_variable }} |
{% 用于执行语句
{{ 输出语句结果到模板
{{}}中的运算
| 12345678 | {{ 1 + 1 }}{{ 3 – 2 }}{{ 1 / 2 }} is {{ 0.5 }}.{{ 20 // 7 }} is 2{{ 11 % 7 }} is 4{{ 2 * 2 }} would return 4{{ \’=\’ * 80 }}{{ 2**3 }} |
变量
| 1234567891011121314 | 变量输出 – 获取属性的方法,都可以{{ foo.bar }}# 查属性 – 查子元素 – 没有报错 {{ foo[\’bar\’] }}# 查子元素 – 查属性 – 没有报错 变量赋值{% set navigation = [(\’index.html\’, \’Index\’), (\’about.html\’, \’About\’)] %}{% set key, value = call_something() %} # set and use it later{% set mybool = [False] %}{% set _ = mybool.append(not mybool.pop()) %} |
注释
| 12345 | {# note: disabled template because we no longer use this {% for user in users %} ... {% endfor %}#} |
空格控制
Jinja2的一些使用摘要
Jinja2作为flask的默认页面模板,感觉蛮强大的,可适用于各种模板类相关的项目
特别是模板继承,写页面很舒服
资源
文档 http://jinja.pocoo.org/docs/
安装
| 12 | sudo easy_install jinja2sudo pip install jinja2 |
终端版helloworld
| 1234 | >>> from jinja2 import Template>>> template = Template(\’Hello {{ name }}!\’)>>> template.render(name=\’World\’)u\’Hello World!\’ |
{%和{{
| 1234567 | id=\”navigation\”> {% for item in navigation %} href=\”{{ item.href }}\”>{{ item.caption }} {% endfor %} My Webpage {{ a_variable }} |
{% 用于执行语句
{{ 输出语句结果到模板
{{}}中的运算
| 12345678 | {{ 1 + 1 }}{{ 3 – 2 }}{{ 1 / 2 }} is {{ 0.5 }}.{{ 20 // 7 }} is 2{{ 11 % 7 }} is 4{{ 2 * 2 }} would return 4{{ \’=\’ * 80 }}{{ 2**3 }} |
变量
| 1234567891011121314 | 变量输出 – 获取属性的方法,都可以{{ foo.bar }}# 查属性 – 查子元素 – 没有报错 {{ foo[\’bar\’] }}# 查子元素 – 查属性 – 没有报错 变量赋值{% set navigation = [(\’index.html\’, \’Index\’), (\’about.html\’, \’About\’)] %}{% set key, value = call_something() %} # set and use it later{% set mybool = [False] %}{% set _ = mybool.append(not mybool.pop()) %} |
注释
| 12345 | {# note: disabled template because we no longer use this {% for user in users %} ... {% endfor %}#} |
空格控制
b3826e44e506179577-2\”> {% for item in