【问题标题】:Accessing the value of a specific index of a list for use in an IF statement (Freemarker)访问列表的特定索引的值以在 IF 语句中使用 (Freemarker)
【发布时间】:2018-01-03 12:22:20
【问题描述】:

我正在尝试访问列表对象 dfeNumber 的当前索引的值,检查它是否是该数字的第一个实例,并添加一个视觉中断。我以前从未在 Freemarker 中工作过(主要是周围的 css),并且花了一些时间查看 Apache 手册,但我找不到这种用法的示例。我认为它看起来类似于:

<#assign msaReached=0>

<#list invName as invNames>

<#if ${dfeNumber[invNames_index]} = "900"> 
  <#assign msaReached++>
  <#if msaReached=1>
    -- do stuff --
  </#if>
</#if>

谁能指出我在场景中使用的语法示例?任何帮助将不胜感激。

【问题讨论】:

    标签: freemarker bfo


    【解决方案1】:

    经过反复试验,我意识到我从未使用过这种组合:

    <#if dfeNumber[invNames_index] = "900">
    

    哪个有效。

    【讨论】:

    • #if 的文档说你需要在那里放置一个“表达式计算为布尔值”,然后还有一章关于表达式。将两者放在一起,您会得到这个,等等。另外,不要使用旧的 FreeMarker 版本。有一段时间您会收到此错误消息,这可以节省您的时间:You can't use "${" here as you are already in FreeMarker-expression-mode. Thus, instead of ${myExpression}, just write myExpression. (${...} is only needed where otherwise static text is expected, i.e, outside FreeMarker tags and ${...}-s.)
    • 对于未来的读者,如果dfeNumber[invNames_index] 是一个数字(而不是一个看起来像数字的字符串),则使用= 900,而不使用"。 (同样== 是规范形式,尽管= 在这里为了向后兼容也这样做。)
    • 感谢您的澄清。在将日志文件重定向到平面文件与使用控制台之后,我能够在日志文件中找到错误消息。这就是让我了解正确语法的原因。另外,还要感谢有关“==”的提示。
    猜你喜欢
    • 2021-08-11
    • 2021-06-27
    • 1970-01-01
    • 1970-01-01
    • 2017-01-07
    • 1970-01-01
    • 2021-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多