WeGa
WeGa

Reputation: 890

How to check, that array has size > 1 in Apache FreeMarker?

I'm trying to implement template with object, that can be plural.

For example: "There is/are n dog(s)." When I tried There <#if dogNames?size>1>is<#else>are</#if> dogNames?size dog<#if dogNames?size>1>s</#if>, I received exception

freemarker.core.NonBooleanException: For "#if" condition: Expected a boolean, but this has evaluated to a number (wrapper: f.t.SimpleNumber): ==> dogNames?size

i.e. there is a problem with angle bracket used for comparison.

In this blog it is said that double quotes is enough to escape the bracket, but I'm not managed to do that in Java. When I declared it like this

String template = "There <#if dogNames?size\">\"1>is<#else>are</#if> dogNames?size dog<#if dogNames?size>1>s</#if>";

and called freemarker api, I received exception

freemarker.core.ParseException: Syntax error in template ...:
Encountered "\">\"", but was expecting one of:
"."
".."
<DOT_DOT_LESS>
"..*"
"?"
"??"
"!"
"["
"("
">"
<TERMINATING_EXCLAM>

I'm using freemarker 2.3.28, java 8

Upvotes: 3

Views: 13852

Answers (1)

ddekany
ddekany

Reputation: 31152

You can write <#if dogNames?size gt 1>. (See https://freemarker.apache.org/docs/dgui_template_exp.html#dgui_template_exp_comparison)

Upvotes: 12

Related Questions