1

If i want specific template to stop from rendering anything, I use if:

{% if something %}
HTML markup...
{% endif %}

This however requires wrapping whole Template in if which is not ideal for readbility.

Is there maybe some kind of Twig function which when used stops rendering template in which it is used? Like this:

{% if something %}
    {% do stopRendering() %}
{% endif %}

HTML markup (will show up if we didnt stopped rendering)...

3 Answers 3

1

Depending on what you exactly need to happen, the {% exit %} (docs) tag my be an option for you.

1

If this related to an entire twig template, you could include it inside an if statement:

{% if not devMode %}{% include '_includes/google-analytics.twig' -%}{%  endif %}

If you need to exit a twig template early (based on some value), Oli's answer is what you might want to look at.

1
{% if not currentUser %}{# or whatever #}
    {% exit 401 %}{# Or whatever HTTP status appropriate template it should be #}
{% endif %}

... the rest of your template ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.