Limit string length in FreeMarker

非 Y 不嫁゛ 提交于 2019-12-04 01:32:32

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

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>

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!