限制FreeMarker中的字符串长度 [英] Limit string length in FreeMarker

查看:1766
本文介绍了限制FreeMarker中的字符串长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从FreeMarker中的字符串中获取子字符串。
但是有两个要考虑的因素:

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


  1. 字符串可以为null

  2. 字符串可以短于最大字符串长度

我执行以下操作:

<#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>

我收到freemarker错误说:

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?

推荐答案

经过大量测试后,错误神奇地解决了。必须是业力。

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>

希望它可以帮助其他人

这篇关于限制FreeMarker中的字符串长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆