Peter T.
Peter T.

Reputation: 3315

Use Freemarker macro call within string parameter of another macro

I have a macro A that formats some text <#macro A text>...${text}...</#macro> and another macro that has a parameter accepting text <#macro B x>Another ${x} text</#macro>

I'd like to call B with the x paramter to be some text formatted by A, s.th. like <@B x="<@A text='abc'/>" /> returns Another <@A text='abc'/>

Is this possible somehow?

I tried the ?interpret as suggested here by ddekany - <@B x="<@A text='abc'/>"?interpret /> but this fails with the error:

Expecting a string, date or number here, Expression .... is instead a freemarker.core.Interpret$TemplateProcessorModel

It seems that a macro call in FreeMarker is something different than a function call in other languages.

Upvotes: 5

Views: 3101

Answers (2)

ddekany
ddekany

Reputation: 31112

Macro calls aren't expressions, and hence can't be used inside expression (like a parameter value). Macros are called for their side effects, which is typically printing to the output, and have no return value. Functions (see #function) are called for their return values, and so function calls are expressions. So maybe you need functions, not a macros in this case.

But if you absolutely have to use the output of a macro call in an expression (or of any arbitrary template fragment), then you have to capture the output via <#assign someVar>...</#assign> or <#local someVar>...</#local>. (Beware with #escape. If you re-print the captured output with ${...}, it will be escaped again, so you will need #noescape.)

Upvotes: 6

Peter T.
Peter T.

Reputation: 3315

I found a workaround using assign:

<#assign a><@A text="abc"/></#assign> <@B text=a/>

Anyway, it would be interesting to know if this is possible somehow.

Upvotes: 2

Related Questions