Skip to content

Commit

Permalink
Add documentation for macro named arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Nov 24, 2023
1 parent ebfd0cd commit 8dbb722
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions book/src/template_syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -587,3 +587,57 @@ You can optionally specify the name of the macro in `endmacro`:
```html
{% macro heading(arg) %}<p>{{arg}}</p>{% endmacro heading %}
```

You can also specify arguments by their name (as defined in the macro):

```
{% macro heading(arg, bold) %}
<h1>{{arg}} <b>{{bold}}</b></h1>
{% endmacro %}
{% call heading(bold="something", arg="title") %}
```

You can use whitespace characters around `=`:

```
{% call heading(bold = "something", arg = "title") %}
```

You can mix named and non-named arguments when calling a macro:

```
{% call heading("title", bold="something") %}
```

However please note than named arguments must always come **last**.

In this case, the named arguments will be passed to the related parameter and then the other
arguments will be placed in what remains. Let's explain it in code:

```
{% macro heading(arg1, arg2, arg3, arg4) %}
{% endmacro %}
{% call heading("something", "b", arg4="ah", arg2="title") %}
```

First it'll be replaced like this:

```
arg1 =
arg2 = "title"
arg3 =
arg4 = "ah"
```

Then `arg1` and `arg3` will be filled in the order of the arguments given to the macro call:

```
arg1 = "something"
arg2 = "title"
arg3 = "b"
arg4 = "ah"
```

0 comments on commit 8dbb722

Please sign in to comment.