mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-01 13:07:22 +00:00
tweaked docs for macro
This commit is contained in:
+20
-10
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user