From 21f840a2ad03b23fc9d54f581323b30a3cc9b620 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 17 Nov 2023 14:47:06 +0100 Subject: Add documentation for macro named arguments --- book/src/template_syntax.md | 51 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/book/src/template_syntax.md b/book/src/template_syntax.md index a794d3f..4bd9894 100644 --- a/book/src/template_syntax.md +++ b/book/src/template_syntax.md @@ -564,7 +564,7 @@ You can define macros within your template by using `{% macro name(args) %}`, en You can then call it with `{% call name(args) %}`: -``` +```jinja {% macro heading(arg) %}

{{arg}}

@@ -576,7 +576,7 @@ You can then call it with `{% call name(args) %}`: You can place macros in a separate file and use them in your templates by using `{% import %}`: -``` +```jinja {%- import "macro.html" as scope -%} {% call scope::heading(s) %} @@ -584,6 +584,51 @@ You can place macros in a separate file and use them in your templates by using You can optionally specify the name of the macro in `endmacro`: -```html +```jinja {% macro heading(arg) %}

{{arg}}

{% endmacro heading %} ``` + +You can also specify arguments by their name (as defined in the macro): + +```jinja +{% macro heading(arg, bold) %} + +

{{arg}} {{bold}}

+ +{% endmacro %} + +{% call heading(bold="something", arg="title") %} +``` + +You can use whitespace characters around `=`: + +```jinja +{% 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**. + +Another thing to note, if a named argument is referring to an argument that would +be used for a non-named argument, it will error: + +```jinja +{% macro heading(arg1, arg2, arg3, arg4) %} +{% endmacro %} + +{% call heading("something", "b", arg4="ah", arg2="title") %} +``` + +In here it's invalid because `arg2` is the second argument and would be used by +`"b"`. So either you replace `"b"` with `arg3="b"` or you pass `"title"` before: + +```jinja +{% call heading("something", arg3="b", arg4="ah", arg2="title") %} +{# Equivalent of: #} +{% call heading("something", "title", "b", arg4="ah") %} +``` -- cgit