{# 这是一行注释 #}
{{ name }}
{# 定义变量 #}
{% set age=32 %}
{{ age }}
{# 比较运算 #}
{{ 1 == 1 }}
{{ 1 != 1 }}
{{ 1 > 1 }}
{{ 1 >= 1 }}
{{ 1 < 1 }}
{{ 1 <= 1 }}
{# 逻辑运算 #}
{{ 1 != 1 or 1 >=1 }}
{{ 1 != 1 and 1 >=1 }}
{# 取反 #}
{{ not 1 <= 1 }}
{# 算数运算 #}
{{ 1+1 }}
{{ 1-1 }}
{{ 1*1 }}
{{ 1/1 }}
{{ 1%1 }}
{{ 1**1 }}
{# 成员运算 #}
{{ 1 in [1,2] }}
{{ not 1 in [1,2] }}
{{ 1 not in [1,2] }}
{# 变量判断 #}
{{ name is defined }}
{{ name is undefined }}
{# if判断 #}
{% if name is defined %}
{{ 'name is defined' }}
{% endif %}
{# if elif #}
{% if name is undefined %}
{{ 'name is defined' }}
{% elif name|length > 2 %}
{{'name length gt 2'}}
{% endif %}
{# 三元运算 #}
{{ 1 if 1>2 else 2 }}
{# for 循环 #}
{% for i in ['a','b'] %}
{{ i }}
{% endfor %}
{# 正常for 循环会执行换行操作,可以添加 减号取消换行#}
{% for i in ['a','b'] -%}
{{ i~' ' }}
{%- endfor %}
{% for i in range(10) -%}
{{ i~' ' }}
{%- endfor %}
{# 定义宏,可以理解为是jija2 中定义的函数 #}
{% macro test() %}
rest1 string
{% endmacro %}
{{ test() }}
{# 带参数的宏 #}
{% macro test(name) %}
hello {{ name }}
{% endmacro %}
{{ test('wangendao') }}
{# 带默认值参数的宏 #}
{% macro test(name='zhangsan') %}
hello {{ name }}
{% endmacro %}
{{ test() }}
标签:name,defined,macro,基础,语法,jijia2,endfor,endmacro,test
From: https://www.cnblogs.com/wangend/p/17071054.html