【问题标题】:Freemarker - unable to get variable to increase in valueFreemarker - 无法获得变量以增加价值
【发布时间】:2020-09-07 15:01:24
【问题描述】:

我已经尝试了几个小时来获取一些 freemarker 代码来输出我想要的内容。本质上,我试图让它从产品中获取特定的“属性”,该属性定义了某物的总成本。使用该数字 - 将其乘以所订购商品的数量,然后减去已支付的金额。最终,我试图为订购的每件商品计算这一点,然后将这些数字相加得出应付余额。

                <#list order.items as orderItem>
            <#list orderItem.attributes as attribute>
                    <#if "${attribute.name}" == ("Total Cost")> 
                        <#assign subtotal=orderItem.subtotal?keep_after("$")>                       
                        <#assign total += (attribute.value?number * orderItem.quantity?number - subtotal?number)>
                </#if>
            </#list>
        </#list>
         Total:  ${total}

当我运行它时,我的变量为空(因为我没有正式看到任何错误输出,我假设这意味着我遇到了错误)。 我最初的问题源于定义的变量(orderItem.subtotal)被列为带有美元符号的字符串,因此我必须将其设置为删除它,然后将其用作数字。 attribute.value 始终是一个数字,但被视为字符串。 orderItem.quantity 是一个数字。 我也尝试过使用它:

                    <#list order.items as orderItem>
            <#list orderItem.attributes as attribute>
                    <#if "${attribute.name}" == ("Total Cost")> 
                        <#assign subtotal=orderItem.subtotal?keep_after("$")>                       
                        <#assign total ++ (attribute.value?number * orderItem.quantity?number - subtotal?number)>
                </#if>
            </#list>
        </#list>
         Total:  ${total}

使用 ++ 表达式,情况更糟,导致我的整个模板在生成时都是空白的,而不仅仅是那个变量。

我在这里做错了吗?我似乎无法让这个变量增加以在我的模板上打印出这个总数。

【问题讨论】:

  • &lt;#if "${attribute.name}" == ("Total Cost")&gt; 你在比较字符串对吗? &lt;#if attribute.name == "Total Cost"&gt; 可以。您应该在#list 之前定义总计,以防循环不运行,您至少会看到一些告诉您总计没有改变的内容。如果您有权访问服务器日志,则可以在那里看到错误。如果您在本地运行 tomcat,您应该可以访问服务器的日志/控制台输出
  • "当我运行它时,我的变量为空" 哪个变量为空?请发布order.item 的定义方式,您还可以通过将 if 语句放置在某些位置并打印出一些消息来调试其中的一些内容,这样至少您知道哪些语句失败了
  • @Huangism - 你建议我如何在我的列表之前定义“total”?我已经尝试了分配和本地,当我在列表之前这样做时会出现某种错误,并且我的整个输出最终都是空白的(假设它是因为错误)。当我运行我的第一组时,为 null 的变量是“total”。 Order.Item 由 Ecwid 店面定义为已订购的商品。
  • &lt;#assign total=0 /&gt; 应该可以工作。如果有错误,你需要阅读日志,看看是什么错误。如果您在运行代码后总数为 0,那么您知道它可能由于某种原因没有通过列表。您也可以只在页面上输出内容,以查看您拥有的变量的值是否符合您的预期

标签: variables freemarker


【解决方案1】:

这段代码应该会有所帮助:

[#assign orderItems = [
{
"name":"prod1",
"price":"$1,000.99",
"quantity":"2"
},
{
"name":"prod2",
"price":"$10.00",
"quantity":"3"
},
{
"name":"prod3",
"price":"$20.00",
"quantity":"3"
}
]]
[#assign total = 0]

<br>Total on start: ${total}
[#list orderItems as orderItem]
    <br>Adding cost of ${orderItem.name} (price: ${orderItem.price}, qty: ${orderItem.quantity})
    [#assign total += orderItem.quantity?number * orderItem.price?keep_after("$")?replace(",","")?number]
    <br>total: ${total}
[/#list]
<br>Final total: ${total}

输出:

Total on start: 0 
Adding cost of prod1 (price: $1,000.99, qty: 2) 
total: 2,001.98 
Adding cost of prod2 (price: $10.00, qty: 3) 
total: 2,031.98 
Adding cost of prod3 (price: $20.00, qty: 3) 
total: 2,091.98 
Final total: 2,091.98

【讨论】:

    【解决方案2】:

    这里真正的问题是您看不到错误消息。使用这样的语言要困难得多。如果你不能让开发人员解决这个问题,那就有一个窍门。将您正在处理的模板放在下面的Your actual template comes here! 的位置,现在您将看到错误消息:

    <#attempt>
    <#assign myTemplate><#noparse>
    Your actual template comes here!
    </#noparse></#assign>
    <@myTemplate?interpret />
    <#recover>
      <p>Template error:
      <pre>
      ${.error}
      </pre>
    </#attempt>
    

    当然,这仅在您开发模板时;以后不要这样放在那里。

    在您的第二个示例中,没有 &lt;#assign total ++ otherStuff&gt; 这样的东西,所以这是一个解析错误(您看不到)。 (如有疑问,请查看 FreeMarker 手册。)

    此外,在糟糕的情况下,您可能会发现这项服务很方便: https://try.freemarker.apache.org

    【讨论】:

    • 我尝试将您的模板放在我的模板中,并将问题语言集放在您所说的位置,当它编译时没有输出,不确定问题出在哪里。至于 '' 我做了很多研究得出尝试使用它的结论,并且在我的研究中有几个地方说在这种情况下使用 ++ 是我想要的更多。 Freemarker中不存在吗?它在 Freemarker 手册中显示为赋值运算符。
    • 你的意思是把你的模板放到我的模板里,我猜。很奇怪。由于您的模板介于 &lt;#noparse&gt;&lt;/#noparse&gt; 之间,因此应将解析异常推迟到运行时。 #attempt 之前和之后是否有输出,这也应该是可见的,但也不显示?
    • ++ 运算符开始,是的,它受支持,但您应该在您的情况下使用+=。见这里:freemarker.apache.org/docs/…
    • #attempt 之前和之后的所有内容都可以正常工作,这只是您给我的尝试模板中的任何内容。没有输出。这很奇怪,但我无法控制生成此内容的网络服务器,因此我无法真正看出问题所在。
    • 这只能意味着您代替 Your actual template comes here! 的内容不会失败,只是不会打印任何内容。
    猜你喜欢
    • 2013-01-25
    • 1970-01-01
    • 1970-01-01
    • 2017-05-06
    • 2018-01-04
    • 1970-01-01
    • 1970-01-01
    • 2017-07-05
    • 1970-01-01
    相关资源
    最近更新 更多