user3298846
user3298846

Reputation: 69

How to check if a given value is a number or not in Freemarker?

In freemarker how do I find out if a particular value is a number or not. Is there any specific method to check if a given value is a number or not in freemarker?

<#if (link_data.canonical)!?matches(".*/sites/.*") && (pageData.ar.gP)?has_content >
    <#if (pageData.ar.gP)?is_number >
        <link rel="author" href="https://plus.google.com/${(pageData.ar.gP)!}" />
    <#else>
        <link rel="ar" href="https://plus.google.com/+${(pageData.ar.gP)!}" />
    </#if>
</#if>

The above code does not work for me.

Upvotes: 3

Views: 13910

Answers (3)

George Mihaylov
George Mihaylov

Reputation: 281

You can check if the number is Integer with this function:

<#assign test = 2>
${isInteger(test)?c}
<#function isInteger number>
<#return number?floor == number>
</#function>

returns true

Upvotes: 0

aj_bk
aj_bk

Reputation: 194

Try id?is_number?c or ?is_string?c or ?is_boolean?c

just add ?c at the end

Upvotes: 0

Danalog
Danalog

Reputation: 559

Yeah, Freemarker has some built-ins for that. You can do id?is_number or ?is_string or ?is_boolean, etc.

source: http://freemarker.org/docs/ref_builtins_expert.html#ref_builtin_isType

Upvotes: 10

Related Questions