freemarker

Freemarker: Combining macros and functions


For a specific use case, I need the functionality of a function that can also write output. I can't use macros because my functions return values. Is there a possibility that either functions generate outputs or macros return values? Or are there perhaps other possibilities to generate such a functionality?


Solution

  • It's not possible. It's either a return value, or writing to the output. The closest is to write a macro, and then also set a variable in it with #assign or #global. And then you can establish a convention that it always assigns to the variable called result.

    There's yet another possibility, which probably not ideal for you. Macro calls can have nested content, to which they can pass a values (kind of look variables). So then you could do something like <@myMacro ; it><#assign myResult = it></@>.