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:
- I had to have the two blank lines in the template (one before the
endfor
and one after theendfor
. - 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.
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 %}