<p>The <a href="http://twig.sensiolabs.org/doc/templates.html#whitespace-control">spaceless</a> tag only removes whitespace between HTML tags. You have to use the <a href="http://twig.sensiolabs.org/doc/templates.html#whitespace-control">whitespace control modifier</a> on your tags to remove leading and or trailing whitespace.</p>

<p>Additionally I'd use the <a href="http://twig.sensiolabs.org/doc/templates.html#other-operators">ternary operator</a> for your conditionals (and put the hyphen inside it):</p>

<pre><code>{% for date in entry.d_date %}
    {{  date.showMonth == 1 ? date.date.month ~ '-' : '' -}}
    {{- date.showDate == 1 ? date.date.day ~ '-' : '' -}}
    {{- date.date.year }}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2014-06-23T13:49:24.427Z
2014-06-23T13:59:04.300Z
2014-06-23T13:59:04.300Z
carlcs
<p>I have the following code.</p>

<pre><code>{% spaceless %}
{% for date in entry.d_date %}
    {% if date.showMonth == 1 %}
    {{ date.date.month }}-
    {% endif %}
    {% if date.showDate == 1 %}
    {{ date.date.day }}-
    {% endif %}
    {{ date.date.year }}
{% endfor %}
{% endspaceless %}
</code></pre>

<p>Which outputs 1- 1- 2014</p>

<p>Is there anyway to get rid of the whitespace without touching the indentation? I've used <code>{spaceless}</code> but it's not working.</p>

<templating><datetime><whitespace>
How do I get rid of whitespace when rendering without touching template indentations?
# How do I get rid of whitespace when rendering without touching template indentations?

(Asked by: username_0 on 2014-06-23)

I have the following code.
{% spaceless %}
{% for date in entry.d_date %}
    {% if date.showMonth == 1 %}
    {{ date.date.month }}-
    {% endif %}
    {% if date.showDate == 1 %}
    {{ date.date.day }}-
    {% endif %}
    {{ date.date.year }}
{% endfor %}
{% endspaceless %}

Which outputs 1- 1- 2014
Is there anyway to get rid of the whitespace without touching the indentation? I've used {spaceless} but it's not working.

**Tags:** templating, datetime, whitespace

---

## 1 answers

### Answer by username_1 on 2014-06-23. Score: 13 | Accepted answer

The spaceless tag only removes whitespace between HTML tags. You have to use the whitespace control modifier on your tags to remove leading and or trailing whitespace.
Additionally I'd use the ternary operator for your conditionals (and put the hyphen inside it):
{% for date in entry.d_date %}
    {{  date.showMonth == 1 ? date.date.month ~ '-' : '' -}}
    {{- date.showDate == 1 ? date.date.day ~ '-' : '' -}}
    {{- date.date.year }}
{% endfor %}


---

