限制 FreeMarker 中的字符串长度

发布于 2024-09-12 10:07:07 字数 1789 浏览 4 评论 0原文

我正在尝试从 FreeMarker 中的字符串中获取子字符串。 但是,有两件事需要考虑:

  1. 字符串可以为空
  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 错误:

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> ...
    "${" ...
    "#{" ...

非常奇怪。 有人可以帮忙吗?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

感情废物 2024-09-19 10:07:07

经过大量测试后,该错误神奇地自行解决。一定是业力。

我的安全检查的最终代码:

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

希望对其他人有帮助

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

My final code for safe checking:

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

Hope it helps somebody else

蓝眼泪 2024-09-19 10:07:07

我确信您很高兴它现在可以工作,但是您收到的错误与您的字符串截断代码无关,这是因为您的 是不正确的。

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


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

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>
雨后咖啡店 2024-09-19 10:07:07

一个更简单的解决方案,无需使用 if-else

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

这将
- 首先在左侧插入空格以确保字符串长度至少为 26 个字符(如果字符串短于 26 个字符)
- 将字符串截断为精确的 26 个字符长(如果字符串超过 26 个字符)

我已经尝试过,它在版本 2.3.24 中运行良好

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

唠甜嗑 2024-09-19 10:07:07

从 Freemarker 2.3.29(2019-08-17 发布)开始,有一个名为 truncate 的新内置函数。根据发行说明

string?truncate(length) 将文本截断为给定长度,如果发生截断,则默认在末尾添加 [...] 。截断发生在单词边界处,除非结果太短,在这种情况下,它会回退到单词中间的截断。还有 ?truncate_w 强制单词边界截断,以及 ?truncate_c (字符边界)不关心单词边界。

所以现在问题可以简单地解决

${minititle?truncate(26)}

As of Freemarker 2.3.29 (released 2019-08-17) there is a new built-in called 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. 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.

So the question can now be solved simply like

${minititle?truncate(26)}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文