Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twig: conditionally return from a macro

I'd like to conditionally return from a macro, like this pseudocode:

{% macro example() %}
    ...
    {% if condition %} {% return %} {% endif %}
    ...
{% endmacro %}

Is this possible with Twig, perhaps with a Twig extension?

like image 593
Ollie Glass Avatar asked Oct 23 '22 07:10

Ollie Glass


1 Answers

There's no return in twig, but you can wrap the rest of your macro in an if not condition block like this:

{% macro example() %}
    ...
    {% if not condition %}
    ...
    {% endif %}
{% endmacro %}

Keep in mind that twig is a templating language.

like image 129
Maerlyn Avatar answered Oct 31 '22 11:10

Maerlyn