# `macro`
Macros are comparable with functions in regular programming languages. Theyare useful to put often used HTML idioms into reusable elements to not repeatyourself.
Here is a small example of a macro that renders a form element:
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3</pre></div></td><td class="code"><div class="highlight"><pre>{% macro input(name, value, type, size) %}
<input type="{{ type|default('text') }}" name="{{ name }}" value="{{ value|e }}" size="{{ size|default(20) }}" />
{% endmacro %}
</pre></div></td></tr></table>
Macros differs from native PHP functions in a few ways:
- Default argument values are defined by using the `default` filter in themacro body;
- Arguments of a macro are always optional.
- If extra positional arguments are passed to a macro, they end up in thespecial `varargs` variable as a list of values.
But as with PHP functions, macros don't have access to the current templatevariables.
Tip
You can pass the whole context as an argument by using the special`_context` variable.
Macros can be defined in any template, and need to be "imported" before beingused (see the documentation for the [*import*](#) tag for moreinformation):
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1</pre></div></td><td class="code"><div class="highlight"><pre>{% import "forms.html" as forms %}
</pre></div></td></tr></table>
The above `import` call imports the "forms.html" file (which can contain onlymacros, or a template and some macros), and import the functions as items ofthe `forms` variable.
The macro can then be called at will:
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2</pre></div></td><td class="code"><div class="highlight"><pre><p>{{ forms.input('username') }}</p>
<p>{{ forms.input('password', null, 'password') }}</p>
</pre></div></td></tr></table>
If macros are defined and used in the same template, you can use thespecial `_self` variable to import them:
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3</pre></div></td><td class="code"><div class="highlight"><pre>{% import _self as forms %}
<p>{{ forms.input('username') }}</p>
</pre></div></td></tr></table>
Warning
When you define a macro in the template where you are going to use it, youmight be tempted to call the macro directly via `_self.input()` insteadof importing it; even if seems to work, this is just a side-effect of thecurrent implementation and it won't work anymore in Twig 2.x.
When you want to use a macro in another macro from the same file, you need toimport it locally:
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
2
3
4
5
6
7
8
9
10
11</pre></div></td><td class="code"><div class="highlight"><pre>{% 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 %}
</pre></div></td></tr></table>
See also
[*from*](#), [*import*](#)
- Twig
- Introduction
- Installation
- Twig for Template Designers
- Twig for Developers
- Extending Twig
- Twig Internals
- Deprecated Features
- Recipes
- Coding Standards
- Tags
- autoescape
- block
- do
- embed
- extends
- filter
- flush
- for
- from
- if
- import
- include
- macro
- sandbox
- set
- spaceless
- use
- verbatim
- Filters
- abs
- batch
- capitalize
- convert_encoding
- date
- date_modify
- default
- escape
- first
- format
- join
- json_encode
- keys
- last
- length
- lower
- merge
- nl2br
- number_format
- raw
- replace
- reverse
- round
- slice
- sort
- split
- striptags
- title
- trim
- upper
- url_encode
- Functions
- attribute
- block
- constant
- cycle
- date
- dump
- include
- max
- min
- parent
- random
- range
- source
- template_from_string
- Tests
- constant
- defined
- divisible by
- empty
- even
- iterable
- null
- odd
- same as