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

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>

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.

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.

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

does freemarker support show all variable in data-model?

I want to see all variables in freemarker data-model, just like struts2 debug tag to show value stack.
Is there a way for freemarker to do this ?
There's no universal solution possible for that, but you can try
<#list .data_model?keys as key>
${key}
</#list>
This works if the data-model is just a usual Map or JavaBean, but for more sophisticated data-models it's up to the data-model implementation if it supports ?keys and if it indeed returns everything.
You also have the variables that you set in the templates, which can be listed like above, only instead of .data_model use .globals, .namespace (which means the current template namespace) and .locals.
You may also have Configuration-level shared variables, and there's no way to list those purely from FTL (you could write a custom TemplateMethodModel for it that reads Configuration.getSharedVariableNames() though, and call it from the template).
Of course, ideally, FreeMarker should have a <#show_variables> directive or something, that does a best effort to show all this... but sadly there is no such thing yet.
An even more detailed way would be this macro:
<#macro dump_object object debug=false>
<#compress>
<#if object??>
<#attempt>
<#if object?is_node>
<#if object?node_type == "text">${object?html}
<#else><${object?node_name}<#if object?node_type=="element" && object.##?has_content><#list object.## as attr>
${attr?node_name}="${attr?html}"</#list></#if>>
<#if object?children?has_content><#list object?children as item>
<#dump_object object=item/></#list><#else>${object}</#if> </${object?node_name}></#if>
<#elseif object?is_method>
#method
<#elseif object?is_sequence>
[<#list object as item><#dump_object object=item/><#if !item?is_last>, </#if></#list>]
<#elseif object?is_hash_ex>
{<#list object as key, item>${key?html}=<#dump_object object=item/><#if !item?is_last>, </#if></#list>}
<#else>
"${object?string?html}"
</#if>
<#recover>
<#if !debug><!-- </#if>LOG: Could not parse object <#if debug><pre>${.error}</pre><#else>--></#if>
</#attempt>
<#else>
null
</#if>
</#compress>
</#macro>
<#dump_object object=.data_model/>
This gives you a full dump of your data model.
Here is #lemhannes macro definition modified to emit JSON. Lightly tested on a fairly simple datamodel
<#macro dump_object object debug=false>
<#compress>
<#if object??>
<#attempt>
<#if object?is_node>
<#if object?node_type == "text">${object?json_string}
<#else>${object?node_name}<#if object?node_type=="element" && object.##?has_content><#list object.## as attr>
"${attr?node_name}":"${attr?json_string}"</#list></#if>
<#if object?children?has_content><#list object?children as item>
<#dump_object object=item/></#list><#else>${object}</#if>"${object?node_name}"</#if>
<#elseif object?is_method>
"#method"
<#elseif object?is_sequence>
[<#list object as item><#dump_object object=item/><#if !item?is_last>, </#if></#list>]
<#elseif object?is_hash_ex>
{<#list object as key, item>"${key?json_string}":<#dump_object object=item/><#if !item?is_last>, </#if></#list>}
<#else>
"${object?string?json_string}"
</#if>
<#recover>
<#if !debug>"<!-- </#if>LOG: Could not parse object <#if debug><pre>${.error}</pre><#else>-->"</#if>
</#attempt>
<#else>
null
</#if>
</#compress>
</#macro>

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