2015-05-21 16 views
0

如果可能的話,將Jade中的兩個字段相乘將會很好。這是我想要實現的:在JADE中的乘法

tbody 
each item in items 
    tr 
    td #{item.sku} 
    td.text-center #{item.price} 
    td.text-center #{item.quantity} 
    //td #{item.quantity} * #{item.price} 
    // - var totals = {item.price} * {item.quantity}; 
    //td #{totals} 
    td #{item.quantity} * #{item.price} 

試過不同的方法,沒有人工作。

回答

4

我相信這是你要找的內容:

tbody 
    each item in items 
    tr 
     td #{item.sku} 
     td.text-center #{item.price} 
     td.text-center #{item.quantity} 
     td.text-right= item.quantity*item.price 
     // - var totalPrice = item.quantity*item.price 
     // td.text-right #{totalPrice} 
     // td.text-right #{item.quantity * item.price} 
     // All three of the above methods work to do what you want to do 

有兩種方式,包括JavaScript插入您的玉石模板,描述here

+0

完美,但現在我想在結果的最後附上一個美元符號D: –

+1

@JoeYahchouchi,我在很長一段時間內沒有使用過玉器,但是我的猜測會是'td.text-right #{item.quantity * item.price} $'會起作用,不是嗎? –

+0

嗨,感謝您的回覆,我昨天用它解決了這個問題 td = product.price * product.quantity +'LL' (LL是我想補充的貨幣) –

1

你可以定義一個乘法函數作爲你的玉本地人的一部分。如果您使用快遞,你可以用app.locals這樣使這可在整個應用程序:

app.js:

app.locals.multiply = function(a, b) { 
    return a * b; 
}; 

玉模板

tbody 
each item in items 
    tr 
    td #{item.sku} 
    td.text-center #{item.price} 
    td.text-center #{item.quantity} 
    //td #{item.quantity} * #{item.price} 
    // - var totals = {item.price} * {item.quantity}; 
    //td #{totals} 
    td #{multiply(item.quantity, item.price)} 

或用玉只:

var helpers = { 
    multiply: function(a, b) { 
     return a * b; 
    } 
} 

var fn = jade.compile('string of jade', options); 
var html = fn(_.extend(yourModel, helpers)); 

Noe,你可能不應該把這麼多的邏輯放在你的模板中。計算總計等任務可能屬於模型對象。這樣做可以讓你在其他視圖中重用它,並針對它編寫單元測試。