diff options
author | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2023-11-17 14:47:06 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2023-11-28 11:36:09 +0100 |
commit | 21f840a2ad03b23fc9d54f581323b30a3cc9b620 (patch) | |
tree | 7ea548b548ab6ee961e2d4a5f8dedc5892bedc8d /book/src/template_syntax.md | |
parent | 28e26751cef5ca5e3b0a0e6e8c8aadff92dc615b (diff) | |
download | askama-21f840a2ad03b23fc9d54f581323b30a3cc9b620.tar.gz askama-21f840a2ad03b23fc9d54f581323b30a3cc9b620.tar.bz2 askama-21f840a2ad03b23fc9d54f581323b30a3cc9b620.zip |
Add documentation for macro named arguments
Diffstat (limited to 'book/src/template_syntax.md')
-rw-r--r-- | book/src/template_syntax.md | 51 |
1 files 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) %} <h1>{{arg}}</h1> @@ -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) %}<p>{{arg}}</p>{% endmacro heading %} ``` + +You can also specify arguments by their name (as defined in the macro): + +```jinja +{% macro heading(arg, bold) %} + +<h1>{{arg}} <b>{{bold}}</b></h1> + +{% 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") %} +``` |