-
Notifications
You must be signed in to change notification settings - Fork 1
macro
Nat! edited this page Mar 1, 2023
·
4 revisions
A macro
is a little bit like a block
, but it can't be overridden. On the
positive side you can give a macro parameters. Blocks are evaluated much later
than macros.
A macro is a compile time construct like a define
. MulleScion deviates
again from TWIG in that there is no imports facility.
{# a macro by itself, produces no output #}
{% macro whatever( text) %}
Here is some {{ text}}.
{% endmacro %}
Output:
This value {{ list }} is from the properties file
Output:
This value (
"0 (value)",
"1 (value)"
) is from the properties file
{% macro table( border, cellpadding, list) %}
<table border="{{border}}" cellpadding="{{ cellpadding }}">
{% for i in list %}
<tr><td >{{ i }}</td></tr>
{% endfor %}
</table>
{% endmacro %}
{{ table( 1, 10, @( @"VfL", @"Bochum", @"1848")) }}
Output:
<table border="1" cellpadding="10">
<tr><td >VfL</td></tr>
<tr><td >Bochum</td></tr>
<tr><td >1848</td></tr>
</table>
And {{ list }} should now be as before the macro expansion
Output:
And (
"0 (value)",
"1 (value)"
) should now be as before the macro expansion
It can be helpful to understand the technical background, how MulleScion does macros. The parser creates objects for commands and expressions. A macro is such a chain, that gets copy/pasted into the current parse situation whenever a macro is called. The parameters are then used to replace identifier values.
Can macro/endmacro be used in multi-line statements ? Preferably not.