Extract and list value define within bracket - freemarker

I am new to freemarker and trying to extract list of value defined within bracket. Example below.. I would like to list values from () so can use it towards assign fucntion.. Couldn't get regex function to work correctly.. Can someone advice?
<alternative-id provider="abc.com" level="episode" description="CHICAGO GO (Sign) (primary) Ep 514">12345678</alternative-id>
<alternative-id provider="abc.com" level="episode" description="NewYork Show (primary) Ep 514">14561234</alternative-id>
<#if model["alternative-id[description=text(\((.*?)\))]"][0]?has_content >
tried regex (((.*?))) to extract the value but don't think freemarker support this. As i am very new to freemarker i am not sure if this is even allowed..

Related

Mapbox - setFilter expression for 'Contains' name within array of possible values

I'm trying filter my Mapbox layer (mapboxgl) to return just places that start with say 'North', 'South, 'East' or 'West'.
My places are in geojson data as a property.
I'm using map.setFilter with an expression to filter the geojson.
Example:
map.setFilter('markers', ["all",filter1,filter2]);
Where filter2 is a simple check on if the place is 'open' or 'closed' and filter1 is my main filter expression explained below.
The main issue is that the name of the place needs to START with one of these values.
So far I can do this IF the strings to check are all the same length by grabbing the length of the strings and then filtering by that substring of the name.
But this doesn't work (obviously) when I have words that are not the same length.
This is pretty simple in most other languages, but my brain cant figure out this in mapbox's expression filters for some reason!
Here's what I have so far (please excuse the poor code quality here, I'm not a professional coder!!):
//the array data to check for a match
filterArray = ['North','South','East','West'];
//checks first values length - not ideal!
var lengthToCheck = filterArray[0].length;
//apply it to the filter to only return ones starting with X
filter1 = ['in', ['slice',['to-string', ['get', 'name']],0,lengthToCheck], ['literal', filterArray]];
I have to slice the name of the place as it might be for example 'Northampton', which wont be found within the array value[0] being 'North'.
This is equally as bad code for the 'East' values as the name slice is going to be 5 characters, so it will be trying something like 'Eastb' (Eastbourne) within the array value 'East' which just wont work.
It would be good if I could some how flip this round so the filter can check the values in the array within the Name, but I can't figure this out!
Is there a way to do that or is there a magic expression feature I'm missing that would solve this problem?
I'm also going to have the same problem with a 'Contains' type check of an array of values too, but I'll cross that bridge once I figure this part out!
Any help appreciated (before I go putting in some convoluted workaround!).

How can I output an XML Node in Freemarker

I'm probably abusing Freemarker here, but I'd like to be able to use it to strip a wrapping element from around the outside of an XML document, something like:
<br:wrap xmlns:br="http://demo.tempuri.com/">
<br:borrower>
<br:id>111-11-1111</br:id>
<br:ssn>111-11-1111</br:ssn>
<br:city>Los Angeles</br:city>
<br:first>John</br:first>
<br:last>Smith</br:last>
<br:phone>310-000-0000</br:phone>
<br:state>CA</br:state>
<br:zip>90025</br:zip>
</br:borrower>
</br:wrap>
I want to remove the outer <wrap/> element. It's easy to select the inner document with:
<#ftl ns_prefixes={"D":"http://demo.tempuri.com"}>
<#assign borrower = doc.wrap.borrower>
{ "result" : "${borrower}" }
The problem here is that the 3rd line is going to result in the good old error:
For "${...}" content: Expected a string or something automatically convertible to string (number, date or boolean), but this has evaluated to a sequence+hash
If I knew exactly what the content and structure of this inner document was I could just walk through it and output the scalar values, but changes all the time. I don't even know the namespaces of all the inner elements (this could even be a problem with the top-level inner object).
I know I could handle this scenario easily with XSLT, but I would much rather find an easy solution in Freemarker.
Any ideas?
${borrower.##markup} should do this.

Displaying default variable if multiple variables are missing in Freemarker

Currently, I have multiple variables:
${top_1}
${top_2}
${top_3}
${top_4}
And I have this conditional logic for a default variable present:
<#if top_1 == "">not available<#else>${top_1}</#if>
How would I include all of the variables and display the default when all fields are missing?
Found the answer.
Expressions like + can tie the multiple variables together. And including the statement at the end of list will include everything without errors.
example:
<#if top_1 + top_2 + top_3 + top_4 == "">not available<#else>${top_1} ${top_2} ${top_3} ${top_4}</#if>
Any kind of styling will work in between that last part of the list as well. Sorry, graphic designer learning Freemarker here.

FreeMarker interpolation results used inline with a second interpolation

Let me start with I'm not a programmer by trade, but I'm learning the best I can. I'm trying to build a template to take the result of one FreeMarker interpolation result and use that as a variable for another. I hope I'm using the terms correctly.
For example, I want the result of (entity.customer.organization.name) to be used in:
${blurb["organizationXXXAttire"]!}
Where XXX is the result of (entity.customer.organization.name)
If it was just a blurb with out a variable company name it would look like:
${blurb["organizationCompanyAttire"]!}
I thought the following would work but it did not:
<#assign organization = (entity.customer.organization.name)>
${blurb["organization<#organization?interpret>Attire"]!}
Thanks in advance for any suggestions.
It's simply ${blurb["organization${entity.customer.organization.name}Attire"]!}.
?interpret is only needed if you have a string that contains a piece of template. Besides you can't call directives (<#...>, <#...>) inside an expression.

change subvariable in FreeMarker

Is there a way to change a subvariable within a hash or a container in a FreeMarker template? For example, suppose I had:
{"name":"Bob", "city":"Detroit", "state":"OH"}
and I want to change the state to "MI". How would I go about doing that? I know about the assign tag, but the documentation says "Note that only top-level variables can be created/replaced". I am unsure as to whether this means subvariables can't be replaced with the assign tag, or subvariables can't be replaced by any means.
I figured out a simple way to do it:
<#assign hash = hash + {"state":"MI"}>

Resources