venkat2010
venkat2010

Reputation: 607

Compare two strings in freemarker

I am using free marker and i am returning response from application as string i need to compare the response with static some string . Here is the way of doing. ${users.isValid} it is returning "true" in but i am not able to compare this variable form my variable.Code i am using is :

<#if ${parameters.isvalid}>
It is valid data
<#else>
It is Invalid data
</#if>

What is possible way to close this issue thanks in advance.

Upvotes: 15

Views: 65967

Answers (4)

Josen Jones
Josen Jones

Reputation: 11

This is neat and worked for me

${("123"=="1234")?string("yees","noo")}

Upvotes: 1

Waqas Ahmed
Waqas Ahmed

Reputation: 5129

In freemarker template you cannot use interpolations inside if statement. It's a frequent mistake to use interpolations on places where they needn't/shouldn't/can't be used. Interpolations work only in text sections (e.g. <h1>Hello ${name}!</h1>) and in string literals (e.g. <#include "/footer/${company}.html">).

A typical WRONG usage is <#if ${big}>...</#if>, which will give syntactical error. You should simply write <#if big>...</#if>. Also, <#if "${big}">...</#if> is WRONG, since it converts the parameter value to string and the if directive wants a boolean value, so it will cause a runtime error.

Upvotes: 1

Manan Shah
Manan Shah

Reputation: 1098

//if isvalid is a string variable...
<#if parameters?? && parameters.isvalid?? && parameters.isvalid="true">
     blah blah
<#else>
     lah lah
</#if>

//if isvalid is a boolean variable...
<#if parameters?? && parameters.isvalid?? && parameters.isvalid=true>
     blah blah
<#else>
     lah lah
</#if>

See the deails

Upvotes: 3

venkat2010
venkat2010

Reputation: 607

I have used the following syntax to compare two string values in freemarker.

<#if parameters.isvalid == "true">

Upvotes: 28

Related Questions