Hello freemarkers gurus
I understand that the difference between freemarker functions and macros is that macros can print to the output, but cannot return values, while functions can return values but cannot print to the output.
Well, I am having a problem because I need both to print and return values:
I am doing recursive tree exploration with freemarker and therefore I have a macro being called recurvively. As the tree is being explored , I need both to print node information to the output, but also compute and return statistics about the nodes explored ( for example the sum of a specific property of the nodes explored)
If I use macro being called recurvively I can print node information but cannot return the statistics to the calling entity.
If I use a function recursively called, I can return the statistics but cannot print node information on the output.
One solution could be to explore the tree twice, once to print node informations and another to collect statistics, but I would hate to use this unelegant solution.
Can someone propose a better solution?
Thanks
Macro variable stores a template fragment (called macro definition body) that can be used as user-defined directive. The variable also stores the name of allowed parameters to the user-defined directive.
Speed versus size The main benefit of using macros is faster execution time. During preprocessing, a macro is expanded (replaced by its definition) inline each time it is used. A function definition occurs only once regardless of how many times it is called.
FreeMarker is a template engine, written in Java, and maintained by the Apache Foundation. We can use the FreeMarker Template Language, also known as FTL, to generate many text-based formats like web pages, email, or XML files.
Or you can even use a global variable as storage for your stats:
<#global stats = [] />
<#-- then when you call your function -->
<#assign = method() />
<#function method param = "">
<#-- do something and before you return you push the stats to the global variable, if you choose my approach of "merging" sequences, be careful that you wrap the new stats item also in a sequence or it will fail miserably =) -->
<#global stats = stats + [{"statvar1": 10, "statvar2": 30}] />
<#return whateveryoulike />
</#function>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With