How to check if a variable exists in a FreeMarker template?
Asked Answered
C

5

203

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 template if the userName variable is supplied, something like:

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

However, the FreeMarker manual seems to indicate that if_exists is deprecated, but I can't find another way to achieve this. Of course, I could simple providing an additional boolean variable isUserName and use that like this:

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

But if there's a way of checking whether userName exists then I can avoid adding this extra variable.

Christology answered 20/11, 2008 at 20:21 Comment(0)
U
365

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?
</#if>

To check if the value exists and is not empty:

<#if userName?has_content>
    Hi ${userName}, How are you?
</#if>
Urceolate answered 20/11, 2008 at 20:25 Comment(4)
In case anyone else was thrown off by this, the #if syntax should be surrounded by less than and greater than characters rather than brackets. for instance: <#if userName??>Contango
It is actually possible to use this syntax, so I just followed the syntax of the question: see freemarker.sourceforge.net/docs/…Urceolate
This only check if the var exists. But if you have an empty String in "userName", the <#if userName??> will return true !! See @user1546081 answer on this page to address this issue.Multidisciplinary
Hello, is there any way to do ${userName!?size} without "if" statement? And ${user!.userName!?size}Contrivance
F
105

This one seems to be a better fit:

<#if userName?has_content>
... do something
</#if>

http://freemarker.sourceforge.net/docs/ref_builtins_expert.html

Fermanagh answered 23/7, 2012 at 14:15 Comment(2)
Seems to check whether the var exists AND if its content is not null or empty (referring to empty Strings here).Multidisciplinary
Have to downvote this because it's technically incorrect and will lead to unintended (false negative) behavior if the variable exists (what op wanted to check for) but is empty.Aragats
U
73

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?
Urceolate answered 20/11, 2008 at 20:33 Comment(2)
The default value operator cannot be used to evaluate to a boolean. As such, it cannot be used in an if statement as op requested.Aragats
Hello, is there any way to do ${userName!?size} without "if" statement? And ${user!.userName!?size}Contrivance
C
6

I think a lot of people are wanting to be able to check to see if their variable is not empty as well as if it exists. I think that checking for existence and emptiness is a good idea in a lot of cases, and makes your template more robust and less prone to silly errors. In other words, if you check to make sure your variable is not null AND not empty before using it, then your template becomes more flexible, because you can throw either a null variable or an empty string into it, and it will work the same in either case.

<#if p?? && p?has_content>1</#if>

Let's say you want to make sure that p is more than just whitespace. Then you could trim it before checking to see if it has_content.

<#if p?? && p?trim?has_content>1</#if>

UPDATE

Please ignore my suggestion -- has_content is all that is needed, as it does a null check along with the empty check. Doing p?? && p?has_content is equivalent to p?has_content, so you may as well just use has_content.

Crumby answered 12/5, 2014 at 18:59 Comment(0)
B
4

For versions previous to FreeMarker 2.3.7

You can not use ?? to handle missing values, the old syntax is:

<#if userName?exists>
   Hi ${userName}, How are you?
</#if>
Bastion answered 6/4, 2018 at 11:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.