簡體   English   中英

在Pebble模板中控制空格

[英]Controlling whitespace in Pebble templates

我很難在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控件修飾符的多種組合,但是在獲取所需格式方面並不走運。

空格控件修改器僅修剪線條。 它不會刪除換行符。 針對您的用例的唯一解決方案是刪除{%endfor%}周圍的空白行

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM