0

我很难在 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"

    },

这有两个问题:

  1. 我必须在模板中有两个空行(一个在 . 之前endfor,一个在endfor.
  2. 我仍然在结束波浪括号之前的输出中得到额外的空白行,即},.

我希望模板看起来更像以下内容:

        "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的组合,但没有获得我想要的格式。

4

1 回答 1

1

空白控制修饰符仅修剪线条。它不会删除换行符。您的用例的唯一解决方案是删除 {% endfor %} 周围的空行

于 2018-07-07T01:56:16.863 回答