1

I'm having a hard time getting the whitespace control the way that I want in a Pebble template. I'm currently generating JSON using Pebble, but this problem and my use case is not specific to JSON (or else I would use a JSON library, such as Jackson instead).

Here's my Pebble template:

        "items": {
            {% for item in items -%}
            "{{ item.name }}": "{{ item.value }}"{% if not loop.last %},{% endif %}

            {% endfor %}

        },

And, here's the generated output:

    "items": {
        "item1": "Value 1",
        "item2": "Value 2"

    },

There are two problems with this:

  1. I had to have the two blank lines in the template (one before the endfor and one after the endfor.
  2. I still end up with the extra blank line in the output before the closing squiggly bracket, i.e. the },.

I'd like for the template to look more like the following:

        "items": {
            {% for item in items -%}
            "{{ item.name }}": "{{ item.value }}"{% if not loop.last %},{% endif %}
            {% endfor %}     
        },

And, I'd like for the resulting output to be:

    "items": {
        "item1": "Value 1",
        "item2": "Value 2"
    },

I have tried many combinations of the whilespace control modifier, but no luck on getting the format that I want.

Nathan Ward
  • 560
  • 5
  • 16
  • Are you able to configure the Pebble `Builder` being used? If so, calling `newLineTrimming(false)` might help you - that would stop Pebble stripping away newlines that follow a Pebble tag (https://pebbletemplates.io/com/mitchellbosecke/pebble/PebbleEngine/Builder/#method__public_Builder_newLineTrimming_boolean_enableNewLineTrimming) – Sam Dec 24 '19 at 16:13

1 Answers1

1

Whitespace control modifier only trim lines. It doesn't remove line feed. The only solution for your use case is to remove blank lines around {% endfor %}