In Java I am using a
HashMap<String, String>
which is then available in my freemarker template.
I can access it like this
Time:${candidFieldsList["STD-TIME_Environmental_1"]}
This will extract the value for the key STD-TIME_Environmental_1 from my map, this works fine. Now I need to combine this with a list to reduce the redundant code.
I have a area in my template which need to be repeated 4 times
Time:${candidFieldsList["STD-TIME_Environmental_1"]}
The difference to the other parts are only the number, so i tried to use the list to solve this. But it did not work
<#list 1..4 as x>
Time:${candidFieldsList["STD-TIME_Environmental_"${x}]}
</#list>
Instead of returning the value for the key, it returns a parser exception or the string of this expression.
freemarker.core.ParseException: Encountered "$" at line 4, column 50 in template.ftl.
Was expecting one of:
"]" ...
"." ...
"[" ...
"(" ...
"?" ...
"!" ...
Try using the + operator to concatenate the strings:
<#list 1..4 as x>
Time:${candidFieldsList["STD-TIME_Environmental_" + x]}
</#list>
Related
I am trying to write a Numeric and Alphabetic Series in Free-marker. However I am not able to implement it.
I have tried various portal and Freemarker website itself, but was not able to find a proper solution.
<#assign count = 0>
<#assign seq = ['a','b','c','d','e','f',]>
<#list params_list as test_param>
${count} ${seq[count]}
<#assign count = count + 1>
</#list>
It will print data in
1 a
2 b
3 c
You can use ?lower_abc (or ?upper_abc) to convert a number to a letter, where 1 corresponds to letter "a". If this is inside #list, then you can get the 1-based item counter with itemVariable?counter. For example:
<#list items as item>
${item?counter} ${item?counter?lower_abc}
</#list>
I am trying to split an input string based on space .
i/p : "darshan kamat"
I need to split above string into 2 different tags in FTL .
expected o/p :
<fname>darshan</fname>
<lname>kamat</lname>
But with split i am just able to iterate print same output which does not help
<#list name?split("\\s+", "r") as x>
${x}
</#list>
With <#assign var= List> throws error as it expects string only
How to store and access list, something like {x[0]} ,{x[1]}
Any pointers please as how to approach?
According to freemarker FAQ I can use any variable name with no limitation:
FreeMarker has no limitations regarding the characters used in variable names, nor regarding the length of the variable names
How can I define in freemarker a variable name as empty string and reference it as
${}
Exception:
freemarker.core.ParseException: Syntax error in template "test.ftlh" in line 1, column 4:
Encountered "}", but was expecting one of:
<STRING_LITERAL>
<RAW_STRING>
"false"
"true"
<INTEGER>
<DECIMAL>
"."
"+"
"-"
"!"
"["
"("
"{"
<ID>
at freemarker.core.FMParser.generateParseException(FMParser.java:5749)
at freemarker.core.FMParser.jj_consume_token(FMParser.java:5608)
at freemarker.core.FMParser.UnaryExpression(FMParser.java:658)
at freemarker.core.FMParser.MultiplicativeExpression(FMParser.java:768)
at freemarker.core.FMParser.AdditiveExpression(FMParser.java:720)
at freemarker.core.FMParser.RangeExpression(FMParser.java:900)
at freemarker.core.FMParser.RelationalExpression(FMParser.java:848)
at freemarker.core.FMParser.EqualityExpression(FMParser.java:811)
at freemarker.core.FMParser.AndExpression(FMParser.java:967)
at freemarker.core.FMParser.OrExpression(FMParser.java:989)
at freemarker.core.FMParser.Expression(FMParser.java:548)
at freemarker.core.FMParser.StringOutput(FMParser.java:1522)
at freemarker.core.FMParser.MixedContentElements(FMParser.java:3721)
at freemarker.core.FMParser.Root(FMParser.java:4411)
at freemarker.template.Template.<init>(Template.java:252)
at freemarker.cache.TemplateCache.loadTemplate(TemplateCache.java:549)
at freemarker.cache.TemplateCache.getTemplateInternal(TemplateCache.java:439)
at freemarker.cache.TemplateCache.getTemplate(TemplateCache.java:292)
at freemarker.template.Configuration.getTemplate(Configuration.java:2654)
at freemarker.template.Configuration.getTemplate(Configuration.java:2503)
at com.FreemarkerTest.main(FreemarkerTest.java:46)
I also tried ${""} by assigning empty variable but it ignores the value assigned:
<#assign ""="aa">
${""}
Normally in a map I can assign a variable with empty string and get it.
While all kind of variable names are allowed (in the data-model at least), they aren't necessarily convenient to refer to. In this case you had to write ${.vars['']}.
Note that the syntax is not "${" VariableName "}", but the more generic "${" Expression "}", so you can write things like ${x + 1}, or even ${1 + 1} (no variable reference in it at all). Thus ${} contains an empty expression (which is invalid), not a variable reference of any kind.
I have a table that has a field that contains a comma separated list of order IDs. What I am trying to do is output each of those IDs separately so that I can use them to look up their corresponding order details in another table.
Does anyone know of a good way to do this?
So far I have tried:
<#data Alerts_test_table as alerts_test>
<#filter
CUSTOMER_ID_=CONTACTS_LIST.CUSTOMER_ID_1>
<#fields AD_ID_LIST>
<#assign seq = ['${alerts_test.AD_ID_LIST}']>
<#list seq?chunk(1) as row><#list row as cell>
${cell}
</#list> </#list>
</#data>
But this just outputs it as a line of text.
Let's say you have comma separated ID-s in idsString, and you want to iterate though the ID-s one by one. Then:
Data-model (with http://try.freemarker.org/ syntax):
idsString = "11, 22, 33"
Template:
<#list idsString?split(r'\s*,\s*', 'r') as idString>
${idString}
</#list>
However, in the template you have posted I see many strange things, so some ideas/pointers:
Be sure that alerts_test.AD_ID_LIST is indeed a String, not a List that you can list directly, without ?split-ing. If it's a String, then '${alerts_test.AD_ID_LIST}' is ultimately the same as alerts_test.AD_ID_LIST. In general, you never need an empty string literal and an ${} in it. It's not useful (but sometimes harmful, as it converts non-string values to string).
?chunk(1) is not useful. The point of ?chunk is to slice a list to smaller lists, but if those smaller lists are to be 1 long, then you might as well just list the items of the original list.
There are no such directives as #data and #filter. Is this some forked FreeMarker version?
If the ID is a number that you need to pass to some API that expects a number, then you will have to convert it to number like idString?number.
I'm trying to output prettier numbers from my FreeMarker template in GeoServer:
<#list features as feature>
<#if attribute.name="lon" || attribute.name="lat">
<td>${feature[attribute.name].value?round}</td>
<#else>
<td>${feature[attribute.name].value}</td>
</#if>
</#list>
If I take out the ?round, I get things like "-121.469166666667". I simply wish to format that number a bit, say by rounding it to 4 decimal places.
I've tried a couple things:
${feature[attribute.name].value?number}
${(feature[attribute.name].value)?number.string("0.0000")}
But those complain of "Expected hash.", so I'm feeling like it's just a syntax issue of conveying the string in the hash to the ? operator correctly, so that I'm actually executing methods on the string... but that has stumped me.
If you always want 4 decimals:
${feature[attribute.name].value?string("0.0000")}
If you want at most 4 decimals, then ?string("0.####")
The ?number part is only needed if value is a string. In that case you should write [...].value?number?string("0.0000"). There's no such thing as ?number.string, hence the "expected hash" error message.