# Jinja syntax and line break

**URL:** <https://forum.ansible.com/t/jinja-syntax-and-line-break/45053>\
**Category:** Get Help\
**Created:** [January 5, 2026, 1:48pm UTC](https://forum.ansible.com/t/jinja-syntax-and-line-break/45053 "2026-01-05T13:48:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chumi](https://avatars.discourse-cdn.com/v4/letter/c/f08c70/32.png) [@chumi](https://forum.ansible.com/u/chumi)\
**Post date:** [January 5, 2026, 1:48pm UTC](https://forum.ansible.com/t/jinja-syntax-and-line-break/45053/1 "2026-01-05T13:48:21Z")

</div>

Hi

i’ve got:

```auto
var1:
    - a1
    - b1

```

```auto
var2:
    - a2
    - b2

```

in a template, i copy the content : {{ foo\_content }}

i’d like to get the content of the file as:

```auto
a1 b1
a2 b2

```

but if

```auto
foo_content:
    blabla {% if var1 | default([]) %} {{ var1 | join(' ') }}{% endif %}
    {% if var2 | default([]) %} {{ var2 | join(' ') }}{% endif %}

```

i’d get :  
`a1 b1 a2 b2`

i do not want to use:

```auto
foo_content:
    blabla {% if var1 | default([]) %} {{ var1 | join(' ') }}{% endif %}

    {% if var2 | default([]) %} {{ var2 | join(' ') }}{% endif %}

```

to get :

> a1 b1  
> a2 b2

Is it possible to have something compact without space between these two lines:  
foo\_content:  
blabla {% if var1 | default() %} {{ var1 | join(’ ‘) }}{% endif %}  
{% if var2 | default() %} {{ var2 | join(’ ') }}{% endif %}

Thank in advance.

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [January 5, 2026, 4:29pm UTC](https://forum.ansible.com/t/jinja-syntax-and-line-break/45053/2 "2026-01-05T16:29:01Z")

</div>

> **[trim\_blocks - Template Designer Documentation — Jinja Documentation (3.1.x)](https://jinja.palletsprojects.com/en/stable/templates/#whitespace-control)**
>
> This document describes the syntax and semantics of the template engine and will be most useful as reference to those creating Jinja templates. As the template engine is very flexible, the configuration from the application can be slightly different...

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [January 5, 2026, 6:31pm UTC](https://forum.ansible.com/t/jinja-syntax-and-line-break/45053/3 "2026-01-05T18:31:59Z")

</div>

You’re going to be disappointed I’m afraid.  
You can get something that looks okay at first glance by adding a `+` before the last `%}` on each `endif`. That preserves the new-line which would normally be deleted after a block command. So

```yaml
foo_content: |
    blabla {% if var1 | default([]) %} {{ var1 | join(' ') }}{% endif +%}
    {% if var2 | default([]) %} {{ var2 | join(' ') }}{% endif +%}

```

produces this:

```plaintext
blabla a1 b1
 a2 b2

```

But it isn’t handling your `default([])` correctly. For example, this uses the undefined `var9` and `var8`:

```yaml
foo_content: |
    blabla
    {% if var1 | default([]) %}{{ var1 | join(' ') }}{% endif +%}
    {% if var9 | default([]) %}{{ var9 | join(' ') }}{% endif +%}
    {% if var8 | default([]) %}{{ var8 | join(' ') }}{% endif +%}
    {% if var2 | default([]) %}{{ var2 | join(' ') }}{% endif +%}
    bling

```

and produces unwanted blank lines:

```plaintext
blabla
a1 b1

a2 b2
bling

```

The right answer is: stop fighting your tools and work with them instead, like so:

```yaml
blabla
{% if var1 | default([]) %}
{{ var1 | join(' ') }}
{% endif %}
{% if var9 | default([]) %}
{{ var9 | join(' ') }}
{% endif %}
{% if var8 | default([]) %}
{{ var81 | join(' ') }}
{% endif %}
{% if var2 | default([]) %}
{{ var2 | join(' ') }}
{% endif %}
bling

```

which produces the desired result

```plaintext
blabla
a1 b1
a2 b2
bling

```

Or this more general approach which produces the same result as the previous example:

```yaml
foo_content: |
    blabla
    {% for v in [var1, var9, var8, var2] %}
    {% if v | default([]) %}
    {{ v | join(' ') }}
    {% endif %}
    {% endfor %}
    bling

```

This kind of answer is one that I’m particularly unhappy with.  
User: I want to do X because \<_whatever_\>. How can I do X?  
Help: Do Y instead!  
User: But I want to do X.  
Help: Yeah, me too. X won’t work. Do Y instead.  
User: But…  
Help: I know. (Do Y instead.)

Good luck.
