Freemarker check if variable exists and is different than zero - freemarker

${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>

Related

Summation of values in freemarker

I am new to programming and this is my first time using freemarker. I was wondering if there is a way to get the summation of the values.
My current code is this:
<td>Total Boxes:</td>
<td><#if invtransfer?has_content>
<#if record.custbody_hdr_sort_by == "Item Category">
<#list invtransfer?sort_by("x.item.class?split(':')[0]?trim") as x>
<#assign currCategory>${x.item.class?split(':')[0]?trim}</#assign>
<#if currCategory == cat>
<#if x.unit == "PIECE">
${(x.item.unitstype?split("/")[0]?trim?number) * (x.quantityuom?replace("-", "")?number)}
<#elseif x.unit == "BOX">
${x.quantityuom?replace("-", "")}
<#elseif x.unit == "PALLET">
${(x.item.unitstype?split("/")[1]?trim?number) * (x.quantityuom?replace("-", "")?number)}
</#if>
</#if>
</#list>
</#if>
</#if>
</td>
and my current output is this:
Total Boxes: 3 4 5
How do I add them all to get Total Boxes: 12?
Please help, Thanks.

How can I check null and empty both in freemarker string?

For example.
String s can have these values like "value", "" or null.
<#if str?? && str?has_content>
${str}
</#if>
Can I check ??(null) and ?has_content(empty not null) both value in freemarker if statement not using TemplateModel?
str?has_content returns true if str is non-null (non-missing), and is also not a 0-length string. So you just need <#if str?has_content>.
(As of TemplateModel-s, every value is a TemplateModel as far as templates see. There's no such thing as a non-TemplateModel value.)

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& .

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.
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

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