How to check if a variable exists in a FreeMarker template?

后端 未结 5 467
轮回少年
轮回少年 2020-12-07 08:42

I have a Freemarker template which contains a bunch of placeholders for which values are supplied when the template is processed. I want to conditionally include part of the

5条回答
  •  爱一瞬间的悲伤
    2020-12-07 09:06

    Also I think if_exists was used like:

    Hi ${userName?if_exists}, How are you?
    

    which will not break if userName is null, the result if null would be:

    Hi , How are you?
    

    if_exists is now deprecated and has been replaced with the default operator ! as in

    Hi ${userName!}, How are you?
    

    the default operator also supports a default value, such as:

    Hi ${userName!"John Doe"}, How are you?
    

提交回复
热议问题