If-else statement inside the freemarker macro parameter?

Is it possible to have an if / else statement inside the freemarker macro parameter?

Now I have:

[#if myForm.id==0]
[#assign action = "add"]
[#else]
[#assign action = "change"]
[/#if]
[@printForm action  /]

      

This is quite a lot of lines of code, can it be shortened a little by putting an if / else construct as a parameter, for example:

[@printForm [if]add[#else]change[/#if]  /]

      

+3


source to share


1 answer


Update: As of FreeMarker 2.3.23, you must use condition?then(whenTrue, whenFalse)

for the ternary operator. It can have a non-stringy result and lazy-evaluates its parameters.

In FreeMarker, you can refer to this as a boolean formatting task: [@printForm (myForm.id == 0)?string('add', 'change') /]



Update: Here's a complete working example:

[#ftl]
${.version}

[#macro printForm s]
s: ${s}
[/#macro] 

[#assign myForm = { "id": 0 } ]

[@printForm (myForm.id == 0)?string('add', 'change') /]

      

+2


source







All Articles