Wednesday, 29 June 2016

To check if object attribute exist or not in Freemarker


Starting from freemarker 2.3.7, you can use this syntax :

${(object.attribute)!}

or, if you want display a default text when the attribute is null :

${(object.attribute)!"some text”}

OR you can use the ??  test operator:

This checks if the attribute of the object is not null:

<#if object.attribute??></#if>

This checks if object or attribute is not null:

<#if (object.attribute)??></#if>

OR you can use the ?has_content operator :

<#if object.attribute?has_content>
          Attribute exist!!
</#if>

OR you can use the _exists operator :

<#if_exists object.attribute>
  Attribute exists = ${ object.attribute} 

</#if_exists>

Use of Freemarker :  

Freemarker is being used massively in the Emailing , SMS, Content Feeder and also the View part of the MVC frameworks like Spring.

Maven Dependency : 

<!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.23</version>
</dependency>

Happy to help post your query in the blog.