限制 FreeMarker 中的字符串长度
我正在尝试从 FreeMarker 中的字符串中获取子字符串。 但是,有两件事需要考虑:
- 字符串可以为空
- 字符串可以比最大字符串长度短
我执行以下操作:
<#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>
我收到一个 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:
- The string can be null
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
经过大量测试后,该错误神奇地自行解决。一定是业力。
我的安全检查的最终代码:
希望对其他人有帮助
The error magically solved itself after extensive testing. Must be karma.
My final code for safe checking:
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-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
从 Freemarker 2.3.29(2019-08-17 发布)开始,有一个名为 truncate 的新内置函数。根据发行说明:
所以现在问题可以简单地解决
As of Freemarker 2.3.29 (released 2019-08-17) there is a new built-in called truncate. Per the release notes:
So the question can now be solved simply like