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

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

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 assign value to variable

I'm having trouble while trying to assign value to a variable using Freemarker.
<#if size??>
<#assign num=size?number>
<#if (num>0)>
<#list 0..num-1 as i>
<#if .vars['abc'+i?c] = "test">
<#assign .vars['abc'+i?c] = .vars['abc'+i?c]?replace("test","Test")>
</#if>
</#list>
</#if>
This is the error message: Encountered ".", but was expecting one of:
STRING_LITERAL
RAW_STRING
ID
Can anyone help me with this?
Thank you.
You can only write top-level variables in a FreeMarker template. Also you can't assign to a variable with dynamically constructed name, except with an ?interpret hack: <#"<#assign abc${i?c} = abc${i?c}?reaplce('test', "Test")>"?interpret />. Obviously that's horrid... BTW, what's the use case here? Why do you need to assign to dynamically constructed variable names?

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

Resources