freemarker-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Pascal Proulx (Jira)" <>
Subject [jira] [Commented] (FREEMARKER-107) Hash expansion to macro arguments (Python **kwargs style)
Date Wed, 16 Oct 2019 19:11:00 GMT


Pascal Proulx commented on FREEMARKER-107:

That's right. Thanks for pointing that out.

The hash copies were used in things like extending namespaces (I copied the namespaces - this
was a workaround because #import was avoided), to convert a non-SimpleHash to a SimpleHash
(not sure if that existed already) even when the BeanWrapper uses adapters, as a workaround
for maps coming from the data model which are in effect not read-only (like special context
variables), and generally for API writing purposes - but never used for template writers or
users of the API (I find this is a useful distinction, API writers vs users). Since I already
have a transform for this and it's quite trivial I won't lose sleep if it doesn't make it
into Freemarker, only mentioned as an alternative solution for .args.

Thanks for considering .args, that will certainly help to simplify code. Even though my macros
have many arguments I am not that worried about a few putAll calls either since it doesn't
seem to be the bottleneck. If that becomes an issue somewhere I'll be sure to let you know.




> Hash expansion to macro arguments (Python **kwargs style)
> ---------------------------------------------------------
>                 Key: FREEMARKER-107
>                 URL:
>             Project: Apache Freemarker
>          Issue Type: New Feature
>          Components: engine
>    Affects Versions: 2.3.28
>            Reporter: Pascal Proulx
>            Priority: Major
> Hello,
> We heavily rely on Freemarker macros to build a helper template API, but have had to
make large workarounds for passing contents of hashes as macro arguments, for several years.
(In truth I should have made this ticket much sooner!)
> It would help greatly simplify our work to have hash expansion to macro arguments, like
> {code:java}
> <#macro myMacro arg1 arg2 arg3="value3">...</#macro>
> <#assign myHash = {"arg1":"value1", "arg2":"value2"}>
> <@myMacro **myHash/><#-- the hash contents are passed as parameters, instead
of the hash itself -->
> {code}
> This exists in Python:
> {code:java}
> def test_var_args_call(arg1, arg2, arg3):
>     pass
> kwargs = {"arg3": 3, "arg2": "two"}
> test_var_args_call(1, **kwargs)
> {code}
> Essentially the hash contents fill in any arguments not explicitly specified.
> For the case where arguments are specified in addition to the hash, you may need to decide
on a good syntax, e.g.:
> {code:java}
> <@myMacro arg1="value1" **myHash/>{code}
> This example doesn't have much precedent in freemarker syntax but is fairly understandable.
> Although we don't need it nearly as much, the same could be done with lists and function
> {code:java}
> <#function myFunc arg1 arg2>...</#function>
> <#assign myList = ["val1", "val2"]>
> ${myFunc(*myList)}
> <#assign myList = ["val2"]>
> ${myFunc("val1", *myList)}
> {code}
> Again similar to Python:
> {code:java}
> def test_var_args_call(arg1, arg2, arg3):
>     pass
> args = ("two", 3)
> test_var_args_call(1, *args)
> {code}
> You might want this for consistency, although in practice the hash expansion will be
many times more useful to us.
> If there's a lack a manpower I could try to see what I can do digging into the source,
but wanted to bring this up for discussion first. It doesn't appear hard to implement to dump
a hash into the macro args map, but there is defining the syntax.
> We use Freemarker 2.3.28 at the moment.
> Thank you

This message was sent by Atlassian Jira

View raw message