Use twig's {% spaceless %} tag only under certain conditions 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.
You could do it by buffering your output, i.e.

{% set output %}
{# Your code here #}
{% endset %}

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