Calling a macro inside another macro in Twig

EDIT: As per cr4zydeejay’s answer Feb 7th 2021 the answer was updated to reflect the correct way in Twig 3.x

Twig 3.x

https://twig.symfony.com/doc/3.x/tags/macro.html

When macro usages and definitions are in the same template, you don’t
need to import the macros as they are automatically available under
the special _self variable:

    <p>{{ _self.input('password', '', 'password') }}</p>

    {% macro input(name, value, type = "text", size = 20) %}
        <input type="{{ type }}" name="{{ name }}" value="{{ value|e }}" size="{{ size }}"/>
    {% endmacro %}

Twig 2.x

Unfortunatly Gremo’s answer works, but is not the right way to do it.

When you define a macro in the template where you are going to use it, you might be tempted to call the macro directly via _self.input() instead of importing it; even if seems to work, this is just a side-effect of the current implementation and it won’t work anymore in Twig 2.x.

http://twig.sensiolabs.org/doc/tags/macro.html

Correct way:

    {% macro input(name, value, type, size) %}
        <input type="{{ type|default('text') }}" name="{{ name }}" value="{{ value|e }}" size="{{ size|default(20) }}" />
    {% endmacro %}
    
    {% macro wrapped_input(name, value, type, size) %}
        {% import _self as forms %}
    
        <div class="field">
            {{ forms.input(name, value, type, size) }}
        </div>
    {% endmacro %}

Leave a Comment