如何在Pug中定义内联变量

7

我正在使用Node + Express应用程序中的Pug模板引擎。

我需要在Pug文件中进行一些计算。例如,我有一个对象数组,并且我必须打印所有对象的金额字段的总和,并需要在table中显示所有金额。

为此,我正在使用Pug中提供的each循环。

我尝试像这样:

   div
    each discount in el.Discounts
     if trxn.category != category
      var discountAmount = discount.amount * -1
      var distTotal = distTotal + discount.amount
      p= distTotal

但它并没有生效,我想声明和更新内联变量。如何做到这一点呢?谢谢。

p是什么?你得到了什么输出?你期望得到什么?请分享一些数据。 - Sookie Singh
P是HTML的段落标签。我没有收到任何错误,但也没有得到结果。 - Arpit Kumar
p 不应该在 if 语句之外吗?并且在每个迭代器之前将 discountAmountdistTotal 定义为 0。 - Sookie Singh
1个回答

13
在变量前面应该写“-”。
div
    each discount in el.Discounts
     if trxn.category != category
      - var discountAmount = discount.amount * -1
      - var distTotal = distTotal + discount.amount
      p= distTotal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接