Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit string length in FreeMarker

I'm trying to get a substring from a string in FreeMarker. However there are 2 thigns to consider:

  1. The string can be null
  2. The string can be shorter then the maximum string length

I do the following:

<#list landingpage1.popularItems as row>
    <li>
        <span class="minititle">
            <#assign minititle=(row.title!"")>
            <#if minititle?length &lt; 27>
                ${minititle}
            <#else>
                ${minititle?substring(0,26)} ...
            <#/if>
        </span>
    </li>
</#list>

I get a freemarker error saying:

Failed to load templates: Encountered "</#list>" at line 144, column 65 in landingpage1.ftl.
Was expecting one of:
    <ATTEMPT> ...
    <IF> ...
    <LIST> ...
    <FOREACH> ...
    <SWITCH> ...
    <ASSIGN> ...
    <GLOBALASSIGN> ...
    <LOCALASSIGN> ...
    <INCLUDE> ...
    <IMPORT> ...
    <FUNCTION> ...
    <MACRO> ...
    <TRANSFORM> ...
    <VISIT> ...
    <STOP> ...
    <RETURN> ...
    <CALL> ...
    <SETTING> ...
    <COMPRESS> ...
    <COMMENT> ...
    <TERSE_COMMENT> ...
    <NOPARSE> ...
    <END_IF> ...
    <BREAK> ...
    <SIMPLE_RETURN> ...
    <HALT> ...
    <FLUSH> ...
    <TRIM> ...
    <LTRIM> ...
    <RTRIM> ...
    <NOTRIM> ...
    <SIMPLE_NESTED> ...
    <NESTED> ...
    <SIMPLE_RECURSE> ...
    <RECURSE> ...
    <FALLBACK> ...
    <ESCAPE> ...
    <NOESCAPE> ...
    <UNIFIED_CALL> ...
    <WHITESPACE> ...
    <PRINTABLE_CHARS> ...
    <FALSE_ALERT> ...
    "${" ...
    "#{" ...

Very odd. Can anybody help?

like image 250
Bart Vangeneugden Avatar asked Aug 03 '10 08:08

Bart Vangeneugden


People also ask

Which is better FreeMarker or Thymeleaf?

FreeMarker has all the essential features to generate HTML for most of the cases, and it is easier to learn. So it is not a bad idea if you want to use it. However, Thymeleaf is the way to go if you want to add custom functionality to your templates.

How do you escape characters in FreeMarker?

esc creates a markup output value out of a string value by escaping all special characters in it.

How do you convert boolean to string in FreeMarker?

string (when used with a boolean value)Converts a boolean to a string. You can use it in two ways: As foo? string("yes", "no") : Formats the boolean value to the first parameter (here: "yes" ) if the boolean is true, and to the second parameter (here: "no" ) if it's false.

What is eval in FreeMarker?

eval. This built-in evaluates a string as an FTL expression. For example "1+2"? eval returns the number 3. (To render a template that's stored in a string, use the interpret built-in instead.)


3 Answers

an even easier solution without using if-else

${minititle?left_pad(26)[0..*26]}

this will - first insert white space on left to ensure the string is at least 26 char long (if the string is short than 26 char) - truncate string to exact 26 char long (if the string is longer than 26 char)

I've tried and it worked well with VERSION 2.3.24

like image 128
Sandy Xiao Avatar answered Oct 23 '22 21:10

Sandy Xiao


The error magically solved itself after extensive testing. Must be karma.

My final code for safe checking:

<#assign minititle=(row.title!"")>
<#if minititle?length &lt; 27>
${minititle}
<#else>
${minititle?substring(0,26)} ...
</#if>

Hope it helps somebody else

like image 29
Bart Vangeneugden Avatar answered Oct 23 '22 21:10

Bart Vangeneugden


I'm sure you're happy it's working now, but the error you were receiving had nothing to do with your String Truncation Code, it's because your </#if> is incorrect.

<#if condition >
    This Is Correct
</#if>


<#if condition >
    This Will Show An Error
<#/if>
like image 7
RockMeetHardplace Avatar answered Oct 23 '22 22:10

RockMeetHardplace