``import`` ========== Twig supports putting often used code into :doc:`macros<../tags/macro>`. These macros can go into different templates and get imported from there. There are two ways to import templates. You can import the complete template into a variable or request specific macros from it. Imagine we have a helper module that renders forms (called ``forms.html``): .. code-block:: jinja {% macro input(name, value, type, size) %} {% endmacro %} {% macro textarea(name, value, rows, cols) %} {% endmacro %} The easiest and most flexible is importing the whole module into a variable. That way you can access the attributes: .. code-block:: jinja {% import 'forms.html' as forms %}
Username
{{ forms.input('username') }}
Password
{{ forms.input('password', null, 'password') }}

{{ forms.textarea('comment') }}

Alternatively you can import names from the template into the current namespace: .. code-block:: jinja {% from 'forms.html' import input as input_field, textarea %}
Username
{{ input_field('username') }}
Password
{{ input_field('password', '', 'password') }}

{{ textarea('comment') }}

.. tip:: To import macros from the current file, use the special ``_self`` variable for the source. .. seealso:: :doc:`macro<../tags/macro>`, :doc:`from<../tags/from>`