[
https://issues.apache.org/jira/browse/FREEMARKER-107?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16947260#comment-16947260
]
Pascal Proulx edited comment on FREEMARKER-107 at 10/9/19 12:18 AM:
--------------------------------------------------------------------
Anyhow I ran freemarker-2.3.30-20191006.185128-1.jar earlier and it seems to
produce what I'd expect, e.g.
{code:java}
<#macro m a b=4 c=5 f=0 other...>
echo: ${a} ${b} ${c} ${other.d!6} ${other.e!0} ${f}
</#macro>
<@m?spread_args({"b":44, "a":33, "d":66, "e":88}) a=3 d=7 f=9/>{code}
gives:
{code:java}
echo: 3 44 5 7 88 9{code}
so it looks good so far. The real cases involve a lot of delegation of
arguments from macro to another, or maps of options being passed around,
implemented in part using the catch-all args (other...).
-------
Side question: apart from ".locals" (which also contains local variables), is
there a built-in that can read only parameters, says ".args"? I have used the
catch-all args for these things, which works fine, but it would help clarity
and documentation to be able to declare the parameters the normal way on the
macro declaration, but still be able to access the parameters using a map, e.g.:
{code:java}
<#macro m a b=4 c=5 f=0>
<#if b == .args.b>always equal</#if>
</#macro>{code}
(sorry if this is off-topic or another ticket, thanks for your efforts)
was (Author: pplx):
Anyhow I ran freemarker-2.3.30-20191006.185128-1.jar earlier and it seems to
produce what I'd expect, e.g.
{code:java}
<#macro m a b=4 c=5 f=0 other...>
echo: ${a} ${b} ${c} ${other.d!6} ${other.e!0} ${f}
</#macro>
<@m?spread_args({"b":44, "a":33, "d":66, "e":88}) a=3 d=7 f=9/>{code}
gives:
{code:java}
echo: 3 44 5 7 88 9{code}
so it looks good so far. The real cases involve a lot of delegation of
arguments from macro to another, or maps of options being passed around,
implemented in part using the catch-all args (other...).
-------
Side question: apart from ".locals" (which also contains local variables), is
there a built-in that can read only parameters, says ".args"? I have used the
catch-all args for these things, which works fine, but it would help clarity
and documentation to be able to declare the parameters the normal way on the
macro declaration, but still be able to access the parameters using a map, e.g.:
{code:java}
<#macro m a b=4 c=5 f=0>
<#if b == .args.b>always equal</#if>
</#macro>{code}
(sorry if this is off-topic or another ticket, thanks for your efforts)
> Hash expansion to macro arguments (Python **kwargs style)
> ---------------------------------------------------------
>
> Key: FREEMARKER-107
> URL: https://issues.apache.org/jira/browse/FREEMARKER-107
> 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 this:
> {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 arguments:
> {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
(v8.3.4#803005)