[ 
https://issues.apache.org/jira/browse/OFBIZ-12274?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17374298#comment-17374298
 ] 

Xin Wang commented on OFBIZ-12274:
----------------------------------

Hi [~danwatford] ,

I take a look at RenderableFtlMacroCall, the internal process seems difference 
from FtlWriter.processWithArgs which I proposed.

Take makeHyperlinkString for example, with RenderableFtlMacroCall, the 
rendering steps are:

1. Encode parameters into strings, with necessary escaping
2. Concatenate macro name and parameters into a template string, that string 
may be long
3. Let FreeMarker parsing that template string and invoke included macro

With FtlWriter.processWithArgs, the steps are:

1. Wrap parameters using DefaultObjectWrapper, to turn Java data structures 
into FreeMarker ones
2. Pass wrapped parameters into template environment, using 
Environment.setVariable, with a temporary variable name, e.g.: 
$args$makeHyperlinkString
3. Let FreeMarker parsing and invoke a template with mostly fixed length, e.g. 
<@makeHyperlinkString?with_args($args$makeHyperlinkString)/>

I think the improvements introduced by FtlWriter.processWithArgs includes:

1. No escaping is needed, as parameters are passed as FreeMarker data structures
2. The temporary template string is smaller, as no parameter strings are 
included
3. It is future-proofed, if FreeMarker expose a stable Macro API in the future 
(for now, freemarker.core.Macro is deprecated), it is relatively easy to make 
use of it

> Add FtlWriter.processWithArgs
> -----------------------------
>
>                 Key: OFBIZ-12274
>                 URL: https://issues.apache.org/jira/browse/OFBIZ-12274
>             Project: OFBiz
>          Issue Type: Improvement
>    Affects Versions: Trunk
>            Reporter: Xin Wang
>            Priority: Major
>
> When calling FTL macro from Java side is needed, instead of crafting a 
> template string with carefully escaping. with newly added 
> FtlWriter.processWithArgs,  we can pass parameters using an environment 
> variable, and then invoke macro with "with_args" builtin. With this approach, 
> it both makes cleaner code and free of fragile escaping.
> As an example, I also changes MacroFormRenderer.makeHyperlinkString to make 
> use of this new facility.
> I have created a GitHub PR for this: 
> https://github.com/apache/ofbiz-framework/pull/307



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to