【问题标题】:Freemarker PDF/Email template list filterFreemarker PDF/Email 模板列表过滤器
【发布时间】:2021-06-18 15:50:31
【问题描述】:

我想在 Netsuite HTML/电子邮件模板中创建一个过滤项目列表。似乎这行代码使用freemarker无法正常工作。

<#assign xs = [1, -2, 3, 4, -5]>
Positives:
<#list xs?filter(x -> x > 0) as x>${x} </#list>
Negatives:
<#list xs?filter(x -> x < 0) as x>${x} </#list>

你们能给我一些关于如何实现这一目标的见解吗?非常感谢您!我是一个初学者,一直在寻找这个,不幸的是没有运气。

【问题讨论】:

    标签: netsuite freemarker


    【解决方案1】:

    看起来 NetSuite 正在使用 FreeMarker v2.3.26,并且在 2.3.29 中添加了内置的 filter

    这是一种不用filter的方法:

    <#assign xs = [1, -2, 3, 4, -5]>
    <#assign xpos = []>
    <#assign xneg = []>
    
    <#list xs as x>
        <#if x gt 0>
            <#assign xpos = xpos + [x]>
        <#elseif x lt 0>
            <#assign xneg = xneg + [x]>
        </#if>
    </#list>
    
    Positives:
    <#list xpos as x>${x} </#list>
    
    Negatives:
    <#list xneg as x>${x} </#list>
    

    【讨论】:

    • 请注意,在一个列表上进行迭代,该列表是上述大量连接的结果,可能会很慢。这种连接旨在用于将几个列表相加,而不是从长度为 1 的列表中构建一个全新的列表。因此,由于那里没有 ?filter,您也可以将条件放在 #list 中.但是,#sep?index 等内容将无法正常工作。
    猜你喜欢
    • 2013-01-23
    • 2014-05-08
    • 2021-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多