Print only 5 line of each having 32 chars using Freemarker - freemarker

How to print only 5 line of each having 32 chars using Freemarker. Currently i have the below solution. Is there any better way of doing using split or substring
<#assign msg="Tell FreeMarker to convert string to real date-time value Convert date-time value to date-only value Let FreeMarker format it according the date_format setting">
<#assign len=msg?length>
<#list 1..5 as i>
<#assign start=(i-1)*32>
<#assign end=i*32>
<#if (end <len)>
${msg[start..end]}
<#else>
${msg[start..len-1]}
</#if>
</#list>
result is
Tell FreeMarker to convert string
g to real date-time value Convert
t date-time value to date-only va
alue Let FreeMarker format it acc
cording the date_format setting

Like this:
<#list msg?matches(".{1,32}")[0..*5] as row>
${row}
</#list>
Note that the "length limited range" operator, ..*, doesn't give error if the length is less than what you asked for. So even with your approach, you can remove the end assignment and the #if/#else, and just use ${msg[start..*32]}.

Related

How to Write Series in FreeMarker?

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>

Freemarker function string formattting

I have the issue related to string formatting in a function of the freemarker. Let's admit there is the next function:
<#function transformWithSign sign amount>
<#--<#local str = amount?string["########.00"]>-->
<#local str = amount?string>
<#local str += sign?string>
<#return str>
</#function>
So, the commented out line does not work and appears the error is "freemarker.core.NonMethodException: For "...(...)" callee: Expected a method or function, but this has evaluated to a string (wrapper: f.t.SimpleScalar):
==> amount?string [in template "html/invoiceTemplate.ftlh" at line 52, column 23]"
This row works fine:
<#local str = amount?string>
What is wrong there? Or does the freemarker function not work with string formatting?
The problem is that amount is not a number, but a string that looks like a number. Thus, amount?string will just return the original string as is, and that string doesn't support the [] or () operators. If amount was a number (or date, or boolean), then ?string returns a more fancy string that does support those operators.
To demonstrate the problem:
<#assign amount = 123>
<#-- Works: -->
${amount?string}
<#-- Also works: -->
${amount?string["########.00"]}
But (note the quotation marks around 123):
<#assign amount = "123">
<#-- Works: -->
${amount?string}
<#-- FAILS: -->
${amount?string["########.00"]}
Ideally, you should ensure that amount comes as a number. But, you can also ask FreeMarker to convert it if it's a string, although, be sure that the string in amount uses the typical computer language number format (not some localized format):
${amount?number?string["########.00"]}

Loop with odd sequence

How to write loop with odd sequence in Apache FreeMarker template?
for example:
<#list seq as n>
...?
${n_index}
</#list>
As result: 1,3,4,5..
Use the Modulus operator.
<#list seq as n>
<#if n % 2 == 1>
<#-- your code here -->
</#if>
</#list>
Assuming you actually want to print the 1st, 3rd, 5th, etc. item of the sequence, as opposed to filter by the parity of the list item (n) itself...
If the result is 1, 2, etc., then either you actually want the even items, or you want n?counter that is 1-based, not n?index that is 0-based. Assuming the last (plus I also print the item itself):
<#list seq as n>
<#if n?is_odd_item>
${n?counter}: ${n}
</#if>
</#list>
Related page in the manual: http://freemarker.org/docs/ref_builtins_loop_var.html

How to Convert a string to number in freemarker template

I want to convert a string to number in freemarker. I want to put some conditional check based on the value of the number. ?number doesn't seems to work.
Any suggestions?
Sorry, ?number does work fine. I was not able to compare the converted number with another number.
This didn't work for me:
<#assign num = numString?number>
<#if num > 100>
</#if>
When I enclosed (num > 100) inside the brackets it worked:
<#if (num > 100)>
</#if>
Since the comparison was not working, I was assuming that conversion was not happening.
My bad.
In your code, you use the closed bracket, so freemarker is evaluating
<#if num >
you should instead use
<#if num gt 100>
This is discussed at the end of this documentation on if statements
https://freemarker.apache.org/docs/ref_directive_if.html
The reason this is working for some and not others is because of the parentheses, which is also explained at the bottom of the documentation
I think you can use it like this:string?eval
Use the below code
<#if num?string > 100?string>
</#if>
It worked for me.

Convert a hash string to a formatted 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.

Resources