HubSpot / jinjava

Jinja template engine for Java
Apache License 2.0
700 stars 169 forks source link

Support keyword arguments in all functions and filters with optional parameters #504

Open TheWebTech opened 4 years ago

TheWebTech commented 4 years ago

It's easier to explain this by showing a use-case first.

Developer wants to use a filter that has 1 required param, and has 2 optional params |filter(param1, param2, "param3") The developer only wants to specify param 1 and param 3 |filter(param1, "param3") This method would cause param 3 to be interpreted as param 2 and not work how the developer might expect.

Theoretically the developer could do this instead: |filter(param1,undefined,"param3") This has some issues, such as defining something to be undefined, little weird. It also means a ton of typing if you only need 1 optional argument out of say 5.

In at least one case with our filters a developer can now do this: |filter(param1, param3="param3") One advantage of this is if a function ever undergoes a breaking change such as to the ordering of parameters, it won't be an issue for a developer that built this way.

We don't believe this is currently set up to work with all functions and filters with optional parameters. That's the goal of this issue.

liamrharwood commented 4 years ago

related: #500

michaelpro1 commented 3 years ago

If you are interested in changing specific filters to support key-value arguments, take a look at IndentFilter that uses a new AbstractFilter to convert array parameters to map. It also includes some basic parameter conversions, and is dependent on Jinjavadoc of the filter

mgns commented 5 months ago

Is there anything speaking against applying the AbstractFilter to all filters? If not, then it seems a straightforward solution to apply the same concept to the remaining filters. Having support for named arguments would bring Jinjava "closer" to the official Jinja documentation.