> For AI agents: the complete documentation index is available at https://www.tteam.icu/llms.txt, the full documentation bundle is available at https://www.tteam.icu/llms-full.txt.

# jinja语法

## 流程控制

`{% … %}` 使用一对大括号和百分号包裹，中间放置条件判断和循环等流程控制表达式。

### for 循环

在 `pillar` 中添加一个列表:

```
names: ['kobe','lebron','t-mac','wade']
```

格式与 Python 中的 for 循环类似，但必须有一个结束标记来标识循环的结束。循环体中使用变量输出语法来输出内容。

```
{%- for name in pillar['names'] %}
player: {{ name }}
{%- endfor %}
```

对 minion2 应用 state 文件后，目标文件会生成以下内容:

```
player: kobe
player: lebron
player: t-mac
player: wade
```

> 这里的百分号后面多了一个小短线，用于清除空格，详见后面的"格式优化"部分。

针对上面 pillar 中定义的字典，也可以像 Python 中一样进行循环:

```
{%- for type, name in pillar['pets'].items() %}
type: {{ type }}, name: {{ name }}
{%- endfor %}
```

目标文件结果为:

```
type: dog, name: haha
type: cat, name: chouchou
```

### if 条件判断

if 条件判断结构如下:

```
{% if grains['id'] == 'minion' %}
xxx
{% elif xxx %}
xxx
{% else %}
xxx
{% endif %}
```
