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

后端 未结 5 465
轮回少年
轮回少年 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:10

    To check if the value exists:

    [#if userName??]
       Hi ${userName}, How are you?
    [/#if]
    

    Or with the standard freemarker syntax:

    <#if userName??>
       Hi ${userName}, How are you?
    
    

    To check if the value exists and is not empty:

    <#if userName?has_content>
        Hi ${userName}, How are you?
    
    

提交回复
热议问题