Data AccessCore JavaApp FrameworksViewTestingBuildingDeploymentDev ToolsSecurityOpinions

Thursday, December 11, 2008

Check for null in FreeMarker 2.4

With the preview of FreeMarker version 2.4 available, a few enhancements to the template language have really improved FreeMarker's appeal.

Providing a check for "null" is one of the largest enhancements that make it easier for developers to transition to FreeMarker from other view technologies. View the (preview announcement).

Previously in 2.3 and below, a "null" value was essentially treated as a missing variable. The missing value operator was the suggested way of testing for null, as the word "null" is not a reserved word in the template language. Here's a look at the 2.3 documentation on treating undefined variables and null values.

FreeMarker 2.3 and Pre - Missing Value Operator
Assume aVariable is null or not defined.

<#if aVariable??>

If you wanted to check if a string was empty or null in FreeMarker, you might have used has_content, one of the "Seldom used and expert built-ins"

Assume aVariable is null or not defined.

<#if aVariable?has_content>

OR

<#if aVariable!?size > 0 or aVariable!?length > 0>


FreeMarker 2.4 Enhancement
2.4 is now null-aware with other template language enhancements, making null a reserved word in 2.4.

4 comments:

  1. I have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Java Training in Chennai | Core Java Training in Chennai

    Online Java Training Java Online Training | Java J2EE Online Training | JavaEE Training Institute in Chennai

    ReplyDelete
  2. A few enhancements to the template language have really improved FreeMarker's appeal.learn web deisgn

    ReplyDelete
  3. All the blog works are choice. The more I read, the more fall under the spell of them.
    e-commerce website

    ReplyDelete