How to check if a given value is a number or not in Freemarker? - 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.

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

Try id?is_number?c or ?is_string?c or ?is_boolean?c
just add ?c at the end

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

Related

Determine If a String Is Present in a List or Map?

How do I determine if a list or map contains a specific string? For example (pseudo code):
<#if listofItems.contains("random-string") >
the map contains a key called random-string
</#if>
Lists
If it's a list:
<#if listOfItems?seq_contains("random-string")>
...
</#if>
Maps
If it's a map:
<#if someMap["random-string"]??>
...
</#if>
If it's a map and the key contains no special characters:
<#if someMap.randomString??>
...
</#if>
If it's a map and you are looking for the value of a key-value pair:
<#if someMap?values?seq_contains("random-string")>
...
</#if>
I think something of this sort should work
<#if listofItems['random-string']?? >
you are inside if block
</#if>

String replace in FTL with a specific word

I need to replace a specific string in a URL in FTL.
Code 1:
<#assign pageUrlWithParams= "https://sample.com/category?filter=low&navParam=Appliances&skrLocale=en_US&t=1"/>
<#if pageUrlWithParams?? && pageUrlWithParams != '' && pageUrlWithParams?contains("skrLocale")>
<#assign pageUrlWithParams = pageUrlWithParams?replace('skrLocale','')/>
</#if>
${pageUrlWithParams}
Code 2 :
<#assign pageUrlWithParams= "https://sample.com/category?filter=low&navParam=Appliances&skrLocale=en_FR&t=2"/>
<#if pageUrlWithParams?? && pageUrlWithParams != '' && pageUrlWithParams?contains("skrLocale")>
<#assign pageUrlWithParams = pageUrlWithParams?replace('skrLocale','')/>
</#if>
${pageUrlWithParams}
I need to remove "skrLocale=en_US" and "skrLocale=en_FR" from pageUrlWithParams.
Known text is "skrLocale", using this I need to remove "skrLocale=en_US" and "skrLocale=en_FR".
Because of en_US and en_FR, i dont know how to take that that part after equals.
Some suggest an answer please. Thanks in advance
Using regular expressions ('r' as 3rd parameter to ?replace) is the key. Note that due to the complexity of URL syntax, we need to handle two edge cases (skrLocale is the first parameter, and skrLocale is the only parameter), which the below function fulfills. However, it doesn't handle %xx escapes in the parameter name (which you might don't care about):
<#function removeSkrLocale url>
<#return url?replace(r'([&\?])skrLocale=[^&]*&?', '$1', 'r')?remove_ending('?')>
</#function>
${removeSkrLocale(pageUrlWithParams)}
Of course you can do this without #function as well, directly inside the ${}, but it's reusable and more self documenting this way.
I would use a Freemarker function (this is a simple version, can be improved)
<#function remove_param_if_value_equals pageUrlWithParams paramName, paramValues >
<#if pageUrlWithParams?has_content && pageUrlWithParams?contains(paramName)>
<#list paramValues as paramValue>
<#local paramNameAndValue = paramName + "=" + paramValue />
<#local pageUrlWithParams = pageUrlWithParams?replace(paramNameAndValue,'')/>
</#list>
</#if>
<#return pageUrlWithParams />
</#function>
<#assign pageUrlWithParams= "https://sample.com/category?filter=low&navParam=Appliances&skrLocale=en_US&t=1"/>
<#assign pageUrlWithParams = remove_param_if_value_equals(pageUrlWithParams, "skrLocale", ["en_US", "en_FR"]) />
${pageUrlWithParams}
The code that does the business remains isolated and can be altered/improved later on.
(For example there is that double "&" that could be cleaned)
I don't know about FTL. But if you need to replace parameter skrLocale in your URL, I think below is good pattern you can use:
(skrLocale(.*?)&)
With (.*?) is called lazy, it match from skrLocale to nearest &. Therefore, with your case, it will match skrLocale=en_US& and skrLocale=en_FR& .

freemarker looping sequence error

I'm trying to retrieve data with this code
<tests>
<#if tests?exists>
<#list tests as object >
<test>
<#list object?keys as key >
<${key}>
<#if object[key]?exists>
<#if object[key]?is_hash> HASH
<#elseif object[key]?is_sequence>
<#list object[key] as hashKey>
</#list>
<#else> ${object[key]}</#if><#else>null
</#if>
</${key}>
</#list>
</test>
</#list>
</#if>
but getting an error ?size is unsupported for: freemarker.ext.beans.SimpleMethodModel
but <#elseif object[key]?is_sequence> sequence returns sequence. As I understand means that my object[key] is a sequence.
Any ideas?
That error occurs because as a result of some historical mishap, Java methods are sequences (with the built in ObjectWrapper-s, that is), but they don't implement ?size. (They are sequences so that foo.m[x] is equivalent with foo.m(x)). Add && !something?is_method to the condition avoid this.

Freemarker check if variable exists and is different than zero

${something.id!} can help me check the variable exists. But what if I also want to check if it is not 0?
[#if something.id?? && something.id!=0]
${something.id}
[/#if]
Or with the standard freemarker syntax:
<#if something.id?? && something.id!=0>
${something.id}
</#if>
You could use an if statement such as
<#assign id0=0>
<#assign id1=1>
id0=<#if id0?? && id0 != 0>${id0}</#if>,
id1=<#if id1?? && id1 != 0>${id1}</#if>,
idx=<#if idx?? && idx != 0>${idx}</#if>
Output
id0=, id1=1, idx=
Or better yet use a function. This function uses a default of zero for the value so that it can handle missing/null values. It will return a zero length string if the value is zero or null otherwise the original value.
<#function existsNotZero value=0>
<#if value == 0>
<#return "">
<#else>
<#return value>
</#if>
</#function>
<#assign id0=0>
<#assign id1=1>
id0=${existsNotZero(id0)},
id1=${existsNotZero(id1)},
idx=${existsNotZero(idx)}
Output
id0=, id1=1, idx=
Like this:
<#if (something.id!0) != 0>${something.id}</#if>

regular expression in freemarker to check if first character in a string is lower case

I am using freemarker to generate a Java class. I am stuck to convert a first character of a string to lower case.
Following is what i tryed but no luck :(
<#function methodName attName >
<#if attName?length > 1 >
<#if attrName(0)?matches([a-z])>
<#return attName>
</#if>
</#if>
</#function>
Thanks.
Try uncap_first:
${"Test"?uncap_first} yields test

Resources