我很难在 Pebble 模板中以我想要的方式获得空白控制。我目前正在使用 Pebble 生成 JSON,但是这个问题和我的用例并不特定于 JSON(否则我会使用 JSON 库,例如 Jackson)。
这是我的 Pebble 模板:
"items": {
{% for item in items -%}
"{{ item.name }}": "{{ item.value }}"{% if not loop.last %},{% endif %}
{% endfor %}
},
而且,这是生成的输出:
"items": {
"item1": "Value 1",
"item2": "Value 2"
},
这有两个问题:
- 我必须在模板中有两个空行(一个在 . 之前
endfor
,一个在endfor
. - 我仍然在结束波浪括号之前的输出中得到额外的空白行,即
},
.
我希望模板看起来更像以下内容:
"items": {
{% for item in items -%}
"{{ item.name }}": "{{ item.value }}"{% if not loop.last %},{% endif %}
{% endfor %}
},
而且,我希望得到的输出是:
"items": {
"item1": "Value 1",
"item2": "Value 2"
},
我尝试了许多whilespace control modifier的组合,但没有获得我想要的格式。