macro, nested, return
Synopsis
Where:
-
name: name of macro variable. It’s not an expression. It follows the same syntax as like top-level variable references, like
myMacro
ormy\-macro
. However, it can also be written as a string literal, which is useful if the macro name contains characters that can’t be specified in an identifier, for example<#macro "foo~bar">...
. Note that this string literal does not expand interpolations (as"${foo}"
). -
param1, param2, …etc.: the name of the local variables store the parameter values (not expression), optionally followed by
=
and the default value (that’s an expression). The default value can even be another parameter, for example<#macro section title label=title>
. The parameter name uses the same syntax as like [top-level variable references], so the same features and restrictions apply. -
paramN, the last parameter may optionally has 3 trailing dots (
...
), which indicates that the macro takes a variable number of parameters and the parameters that doesn’t match any other parameters will be collected in this last parameter (also called the catch-all parameter). When the macro is called with named parameters, paramN will be a hash containing all of the undeclared key/value pairs passed to the macro. When the macro is called using positional parameters, paramN will be the sequence of the extra parameter values. (Inside the macro, to find out which was the case, you can usemyCatchAllParam?is_sequence
.) -
loopvar1, loopvar2, …etc.: Optional. The values of loop variables that the
nested
directive wants to create for the nested content. These are expressions.
The return
and nested
directives are optional and can be used anywhere and for any times between the <#macro ...>
and </#macro>
.
Parameters without default value must precede parameters with default value (paramName=defaultValue
).
Description
Creates a macro variable (in the current namespace, if you know namespace feature). If you are new to macros and user-defined directives you should read the the tutorial about user-defined directives.
Macro variable stores a template fragment (called macro definition body) that can be used as user-defined directive. The variable also stores the name of allowed parameters to the user-defined directive. You must give value for all of those parameters when you use the variable as directive, except for parameters that has a default value. The default value will be used if and only if you don’t give value for the parameter when you call the macro.
The variable will be created at the beginning of the template; it does not mater where the macro
directive is placed in the template. Thus, this will work:
Example: Macro without parameters:
Output:
Example: Macro with parameters:
Output:
Example: Macro with parameters and default parameter values:
Output:
Example: A more complex macro.
Output:
Example: A macro with support for a variable number of named parameters:
Output:
Example: A macro that supports a variable number of positional parameters, regardless if it uses named or positional parameter passing:
Output:
nested
The nested
directive executes the template fragment between the start-tag and end-tags of the user-defined directive. The nested part can contain anything what is valid in templates; interpolations, directives, …etc. It is executed in the context where the macro was called from, rather than in the context of the macro definition body. Thus, for example, you don’t see the local variables of the macro in the nested part. If you don’t call the nested
directive, the part between the start-tag and end-tags of the user-defined directive will be ignored.
Example:
The nested directive can create loop variables for the nested content. For example:
A more complex example:
return
With the return
directive, you can leave a macro or function definition body anywhere. Example: