*args, **kwargs in jinja2 macros

后端 未结 1 942
说谎
说谎 2020-12-13 17:15

How are extra args & kwargs handled for a Jinja2 macro? The documentation isn\'t exactly clear offhand.

For example, this is clearly wrong:

{% macr         


        
相关标签:
1条回答
  • 2020-12-13 17:42

    The trick is that kwargs has to be accessed at least once in any macro that should accept them. That is to say, you must call {{ kwargs }} once in macro body without declaring it in macro argument list. The same is true for {{ varargs }}.

    This will not work

    {% macro example_2(one, two) %}
        * {{one}} - {{two}}
    {% endmacro %}
    {{example_2(1, 2, test="Hello")}}
    

    This will

    {% macro example_2(one, two) %}
        * {{one}} - {{two}}
        * {{kwargs}}
    {% endmacro %}
    {{example_2(1, 2, test="Hello")}}
    
    0 讨论(0)
提交回复
热议问题