@@ -564,7 +564,7 @@ You can define macros within your template by using `{% macro name(args) %}`, en
564
564
565
565
You can then call it with ` {% call name(args) %} ` :
566
566
567
- ```
567
+ ``` jinja
568
568
{% macro heading(arg) %}
569
569
570
570
<h1>{{arg}}</h1>
@@ -576,14 +576,59 @@ You can then call it with `{% call name(args) %}`:
576
576
577
577
You can place macros in a separate file and use them in your templates by using ` {% import %} ` :
578
578
579
- ```
579
+ ``` jinja
580
580
{%- import "macro.html" as scope -%}
581
581
582
582
{% call scope::heading(s) %}
583
583
```
584
584
585
585
You can optionally specify the name of the macro in ` endmacro ` :
586
586
587
- ``` html
587
+ ``` jinja
588
588
{% macro heading(arg) %}<p>{{arg}}</p>{% endmacro heading %}
589
589
```
590
+
591
+ You can also specify arguments by their name (as defined in the macro):
592
+
593
+ ``` jinja
594
+ {% macro heading(arg, bold) %}
595
+
596
+ <h1>{{arg}} <b>{{bold}}</b></h1>
597
+
598
+ {% endmacro %}
599
+
600
+ {% call heading(bold="something", arg="title") %}
601
+ ```
602
+
603
+ You can use whitespace characters around ` = ` :
604
+
605
+ ``` jinja
606
+ {% call heading(bold = "something", arg = "title") %}
607
+ ```
608
+
609
+ You can mix named and non-named arguments when calling a macro:
610
+
611
+ ```
612
+ {% call heading("title", bold="something") %}
613
+ ```
614
+
615
+ However please note than named arguments must always come ** last** .
616
+
617
+ Another thing to note, if a named argument is referring to an argument that would
618
+ be used for a non-named argument, it will error:
619
+
620
+ ``` jinja
621
+ {% macro heading(arg1, arg2, arg3, arg4) %}
622
+ {% endmacro %}
623
+
624
+ {% call heading("something", "b", arg4="ah", arg2="title") %}
625
+ ```
626
+
627
+ In here it's invalid because ` arg2 ` is the second argument and would be used by
628
+ ` "b" ` . So either you replace ` "b" ` with ` arg3="b" ` or you pass ` "title" ` before:
629
+
630
+ ``` jinja
631
+ {% call heading("something", arg3="b", arg4="ah", arg2="title") %}
632
+ {# Equivalent of: #}
633
+ {% call heading("something", "title", "b", arg4="ah") %}
634
+ ```
0 commit comments