企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# `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) %} &lt;input type="{{ type|default('text') }}" name="{{ name }}" value="{{ value|e }}" size="{{ size|default(20) }}" /&gt; {% 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>&lt;p&gt;{{ forms.input('username') }}&lt;/p&gt; &lt;p&gt;{{ forms.input('password', null, 'password') }}&lt;/p&gt; </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 %} &lt;p&gt;{{ forms.input('username') }}&lt;/p&gt; </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) %} &lt;input type="{{ type|default('text') }}" name="{{ name }}" value="{{ value|e }}" size="{{ size|default(20) }}" /&gt; {% endmacro %} {% macro wrapped_input(name, value, type, size) %} {% import _self as forms %} &lt;div class="field"&gt; {{ forms.input(name, value, type, size) }} &lt;/div&gt; {% endmacro %} </pre></div></td></tr></table> See also [*from*](#), [*import*](#)