<p>You could do it by buffering your output, i.e.</p>

<pre><code>{% set output %}
{# Your code here #}
{% endset %}

{% if isDev %}
     {{ output }}
{% else %}
    {% spaceless %}
        {{ output }}
    {% endspaceless %}
{% endif %}
</code></pre>

CC BY-SA 3.0
2016-01-20T11:07:41.677Z
This is a nice solution, I wasn't aware of the `{% set %}{% endset %}` pair
bravokiloecho
CC BY-SA 3.0
2016-07-19T09:37:24.430Z
Thx, I like that idea. I use the untouched HTML for local and dev server and the spaceless option for production.
Mario
CC BY-SA 3.0
2016-01-19T18:29:10.707Z
2016-01-19T18:29:10.707Z
Mats Mikkel Rummelhoff
<p>Yes, Twig (sometimes) complains if you're splitting tags which belong together into different conditionals, I ran into that issue in a similar situation.</p>

<p>But as already noted in the comments, I'm also curious why you want to split the <code>spaceless</code> tag. If it's about minifying HTML, the tag isn't very efficient anyways and I'd recommend to use something like Andrew Welch's <a href="https://github.com/nystudio107/minify">Minify plugin</a> instead.</p>

CC BY-SA 3.0
2016-01-20T11:09:45.643Z
It was about minifying the HTML but I wasn't aware of its lack of efficiency nor of the Minify plugin. Great tip! I will get on to that now, plus I've seen that it has a nice config that disables it for dev envioronments.
bravokiloecho
CC BY-SA 3.0
2016-01-19T17:43:57.170Z
2016-01-19T17:43:57.170Z
carlcs
<p>Is there a way to use Twig's <code>{% spaceless %}</code> tag only under certain conditions?</p>

<p>For example, say you wanted the HTML to be spaceless in the live environment but wanted it uncompressed in the dev environment. I tried something like:</p>

<pre><code>{% if isDev %}
    {% spaceless %}
{% endif %}
{# Some code... #}
{% if isDev %}
    {% endspaceless %}
{% endif %}
</code></pre>

<p>But this just throws a Twig error.</p>

CC BY-SA 3.0
2016-01-19T16:45:49.030Z
What is the Twig error that returns? Also out of curiosity, what would be the reason you would need to change the code on live/dev environments like this?
Jamie Wade
<templating>
Use twig's {% spaceless %} tag only under certain conditions
# Use twig's {% spaceless %} tag only under certain conditions

(Asked by: username_0 on 2016-01-19)

Is there a way to use Twig's {% spaceless %} tag only under certain conditions?
For example, say you wanted the HTML to be spaceless in the live environment but wanted it uncompressed in the dev environment. I tried something like:
{% if isDev %}
    {% spaceless %}
{% endif %}
{# Some code... #}
{% if isDev %}
    {% endspaceless %}
{% endif %}

But this just throws a Twig error.

**Tags:** templating

> **Comments:**
> **username_1**: What is the Twig error that returns? Also out of curiosity, what would be the reason you would need to change the code on live/dev environments like this?  

---

## 2 answers

### Answer by username_4 on 2016-01-19. Score: 6 | Accepted answer

Yes, Twig (sometimes) complains if you're splitting tags which belong together into different conditionals, I ran into that issue in a similar situation.

But as already noted in the comments, I'm also curious why you want to split the `spaceless` tag. If it's about minifying HTML, the tag isn't very efficient anyways and I'd recommend to use something like Andrew Welch's [Minify plugin](https://github.com/nystudio107/minify) instead.

> **Comments:**
> **username_0**: It was about minifying the HTML but I wasn't aware of its lack of efficiency nor of the Minify plugin. Great tip! I will get on to that now, plus I've seen that it has a nice config that disables it for dev envioronments.  

---

### Answer by username_2 on 2016-01-19. Score: 7

You could do it by buffering your output, i.e.
{% set output %}
{# Your code here #}
{% endset %}

{% if isDev %}
     {{ output }}
{% else %}
    {% spaceless %}
        {{ output }}
    {% endspaceless %}
{% endif %}

> **Comments:**
> **username_0**: This is a nice solution, I wasn't aware of the `{% set %}{% endset %}` pair  
> **username_3**: Thx, I like that idea. I use the untouched HTML for local and dev server and the spaceless option for production.  

---

