import

The import tag is used to import macros into the current template.

There are three ways to import a macro into a template.

  1. Import the entire template containing the macros.
  2. Import macros from the template into the current namespace.
  3. Define the macro in the current template and use the special _self variable to import.

For our example let say that we have a template file located at macros/forms.twig within our theme folder. In that template are two macros that output different form field types.

{% macro input(label, name, value, type, size, placeholder) %}
  <label>{{ label }}
    <input type="{{ type|default('text') }}" name="{{ name }}"{% if value %} value="{{ value }}"{% endif %} size="{{ size|default(30) }}{% if placeholder %} placeholder={{ placeholder }}"{% endif %}>
  </label>
{% endmacro %}
{% macro textarea(label, name, value, rows, cols) %}
  <label>{{ label }}
    <textarea name="{{ name }}" rows="{{ rows|default(10) }}" cols="{{ cols|default(50) }}">{{ value }}</textarea>
  </label>
{% endmacro %}

The easiest and most flexible option is to just import the whole macro template into the current template.

{% import 'macros/forms' as forms %}
<form action="/page" method="post">
  <p>{{ forms.input('User Name', 'username') }}</p>
  <p>{{ forms.input('Password', 'password', null, 'password' }}</p>
  <p>{{ forms.input('Your Name', 'name', null, null, 40, 'Enter your full name') }}</p>
  <p>{{ forms.textarea('Address', 'address') }}</p>
</form>

Alternately, you could import names from the template file into the current namespace.

{% from 'macros/forms' import input as inputField, textarea %}
<form action="/page" method="post">
  <p>{{ inputField('User Name', 'username') }}</p>
  <p>{{ inputField('Password', 'password', null, 'password' }}</p>
  <p>{{ inputField('Your Name', 'name', null, null, 40, 'Enter your full name') }}</p>
  <p>{{ textarea('Address', 'address') }}</p>
</form>

Importing macros in the same template file

If you define a macro within the same template file that will use it then you can import all macros in the same template using the _self variable instead of the template name.

{% macro input(label, name, value, type, size, placeholder) %}
  <label>{{ label }}
    <input type="{{ type|default('text') }}" name="{{ name }}"{% if value %} value="{{ value }}"{% endif %} size="{{ size|default(30) }}{% if placeholder %} placeholder={{ placeholder }}"{% endif %}>
  </label>
{% endmacro %}
{% import _self as fields %}
<form action="/page" method="post">
  <p>{{ fields.input('User Name', 'username') }}</p>
  <p>{{ fields.input('Password', 'password', null, 'password' }}</p>
  <p>{{ fields.input('Your Name', 'name', null, null, 40, 'Enter your full name') }}</p>
</form>

Using a macro within another macro

If you want you use a macro within another macro you first need to import the macro locally.

{% macro input(name, value, type, size, placeholder) %}
    <input type="{{ type|default('text') }}" name="{{ name }}"{% if value %} value="{{ value }}"{% endif %} size="{{ size|default(30) }}{% if placeholder %} placeholder={{ placeholder }}"{% endif %}>
{% endmacro %}
{% macro inputField(label, name, value, type, size, placeholder) %}
  {% import _self as fields %}
  <label>{{ label }}
    {{ fields.input(name, value, type, size, placeholder) }}
  </label>
{% endmacro %}

< Back to the list of tags