pallets / jinja

A very fast and expressive template engine.

Home Page:https://jinja.palletsprojects.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support explicitly disabling positional arguments in macros

phemmer opened this issue · comments

Currently when you define a macro, it can be called with positional arguments and named arguments. In normal python you can disable the use of positional arguments by using a asterisk, forcing any further arguments to be named. The proposal is to support the same functionality in Jinja.

For example, python allows:

def foo(bar='bar', *, a=1, b):
    ...

The proposal is to support:

{% macro foo(bar='bar', *, a=1, b) %}
    ...
{% endmacro %}

Commonly in python, this is something like *args and not just a bare *. However Jinja already has the special variable varargs. While support for *args (or any other custom name) could be supported, I figure a bare * would be a simpler change (and forward compatible if *args were to be supported).

Why?

One reason is when you don't want to support positional arguments. For example you might not want to make API guarantees that the position of arguments will remain unchanged.

Another is when you don't want to have to put default arguments at the end. It might make the code more readable to keep related arguments together, instead of having to put the ones without defaults at the front, and the ones with defaults at the end.