4 Twig supports putting often used code into :doc:`macros<../tags/macro>`. These
5 macros can go into different templates and get imported from there.
7 There are two ways to import templates. You can import the complete template
8 into a variable or request specific macros from it.
10 Imagine we have a helper module that renders forms (called ``forms.html``):
14 {% macro input(name, value, type, size) %}
15 <input type="{{ type|default('text') }}" name="{{ name }}" value="{{ value|e }}" size="{{ size|default(20) }}" />
18 {% macro textarea(name, value, rows, cols) %}
19 <textarea name="{{ name }}" rows="{{ rows|default(10) }}" cols="{{ cols|default(40) }}">{{ value|e }}</textarea>
22 The easiest and most flexible is importing the whole module into a variable.
23 That way you can access the attributes:
27 {% import 'forms.html' as forms %}
31 <dd>{{ forms.input('username') }}</dd>
33 <dd>{{ forms.input('password', null, 'password') }}</dd>
35 <p>{{ forms.textarea('comment') }}</p>
37 Alternatively you can import names from the template into the current
42 {% from 'forms.html' import input as input_field, textarea %}
46 <dd>{{ input_field('username') }}</dd>
48 <dd>{{ input_field('password', '', 'password') }}</dd>
50 <p>{{ textarea('comment') }}</p>
54 To import macros from the current file, use the special ``_self`` variable
57 .. seealso:: :doc:`macro<../tags/macro>`, :doc:`from<../tags/from>`