简体   繁体   English

限制 FreeMarker 中的字符串长度

[英]Limit string length in FreeMarker

I'm trying to get a substring from a string in FreeMarker.我正在尝试从 FreeMarker 中的字符串获取 substring。 However there are 2 thigns to consider:但是,有两个问题需要考虑:

  1. The string can be null字符串可以是 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:我收到一个 freemarker 错误说:

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>

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>是不正确的。

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


<#if condition >
    This Will Show An Error
<#/if>

an even easier solution without using if-else 一个更简单的解决方案,而不使用if-else

${minititle?left_pad(26)[0..*26]} $ {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) 这将 - 首先在左边插入空白以确保字符串至少26个字符长(如果字符串短于26个字符) - 截断字符串到26个字符长(如果字符串长于26个字符)

I've tried and it worked well with VERSION 2.3.24 我已经尝试过,它与VERSION 2.3.24配合得很好

As of Freemarker 2.3.29 (released 2019-08-17) there is a new built-in called truncate.从 Freemarker 2.3.29(2019-08-17 发布)开始,有一个名为 truncate 的新内置函数。 Per the release notes :根据发行说明

string?truncate(length) truncates the text to the given length, and by default adds [...] at the end if truncation has happened. string?truncate(length) 将文本截断为给定长度,如果发生截断,默认情况下会在末尾添加 [...] 。 Truncation happens at word boundaries, unless the result is too short that way, in which case it falls back to truncation mid word.截断发生在单词边界,除非结果太短,在这种情况下它会回退到截断中间单词。 There's also?truncate_w to force Word Boundary truncation, and?truncate_c (for Character Boundary) that doesn't care about word boundaries.还有?truncate_w 强制截断字边界,以及?truncate_c(用于字符边界)不关心字边界。

So the question can now be solved simply like所以这个问题现在可以简单地解决

${minititle?truncate(26)}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM