tweaked docs for macro

This commit is contained in:
Fabien Potencier
2019-03-11 13:59:54 +01:00
parent 1da95371d1
commit 04408cda3a
+20 -10
View File
@@ -1,6 +1,11 @@
``macro``
=========
.. versionadded:: 1.12
The possibility to define default values for arguments in the macro
signature was added in Twig 1.12.
Macros are comparable with functions in regular programming languages. They
are useful to put often used HTML idioms into reusable elements to not repeat
yourself.
@@ -9,19 +14,24 @@ Here is a small example of a macro that renders a form element:
.. code-block:: jinja
{% macro input(name, value, type, size) %}
<input type="{{ type|default('text') }}" name="{{ name }}" value="{{ value|e }}" size="{{ size|default(20) }}" />
{% endmacro %}
Default argument values can be defined with the ``default`` filter in the
macro body, as above, or as in native PHP functions:
.. code-block:: jinja
{% macro input(name, value, type = 'text', size = 20) %}
{% macro input(name, value, type = "text", size = 20) %}
<input type="{{ type }}" name="{{ name }}" value="{{ value|e }}" size="{{ size }}" />
{% endmacro %}
Each argument can have a default value (here ``text`` is the default value for
``type`` if not provided in the call).
.. note::
Before Twig 1.12, defining default argument values was done via the
``default`` filter in the macro body:
.. code-block:: jinja
{% macro input(name, value, type, size) %}
<input type="{{ type|default('text') }}" name="{{ name }}" value="{{ value|e }}" size="{{ size|default(20) }}" />
{% endmacro %}
Macros differ from native PHP functions in a few ways:
* Arguments of a macro are always optional.