[英]Pug/Jade and inline javascript calculations
我再次迷失了,試圖在玉模板中做一些簡單的計算。
給定此數據對象:
{
"trade": {
"name": "Mogens",
"dst_currency": "EUR",
"dst_value": 115.7,
"src_price": null,
"src_value": 2,
"src_currency": "XMR",
"date": null
}
}
和這個哈巴狗來源:
table
thead
tr
th Currency
th Quantity
th Price
th Total
th Date
tbody
tr
script.
if (trade.dst_currency === "EUR")
trade.src_price = trade.dst_value / trade.src_value
else
trade.src_price = Number(trade.src_value) / Number(trade.dst_value)
th.align-middle #{trade.dst_currency}
th.align-middle #{trade.dst_value}
th.align-middle= #{trade.src_price}
th.align-middle #{trade.src_value} #{trade.src_currency}
th.align-middle #{trade.date}
if trade.name === "Bob"
h1 Hello Bob
else
h1 My name is #{trade.name}
如果有可能怎么辦? 我想念什么?
好的。 最后想通了。
我不得不放棄內聯腳本,而選擇更簡單的方法。
table
thead
tr
th Currency
th Quantity
th Price
th Total
th Date
tbody
tr
th.align-middle #{trade.dst_currency}
th.align-middle #{trade.dst_value}
if (trade.dst_currency === "EUR")
th.align-middle #{trade.dst_value / trade.src_value}
else
th.align-middle #{trade.src_value / trade.dst_value}
th.align-middle #{trade.src_value} #{trade.src_currency}
th.align-middle #{trade.date}
p.
EUR #{trade.dst_value / trade.src_value}
XMR #{trade.src_value / trade.dst_value}
- var name = "Bobby"
if name == "Bob"
h1 Hello #{name}
else
h1 My name is #{trade.name}, born on #{trade.date}
編譯為
<table>
<thead>
<tr>
<th>Currency</th>
<th>Quantity</th>
<th>Price</th>
<th>Total</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<tr>
<th class="align-middle">EUR</th>
<th class="align-middle">115.7</th>
<th class="align-middle">57.85</th>
<th class="align-middle">2 XMR</th>
<th class="align-middle">29 May, 1958</th>
</tr>
</tbody>
</table>
<p>
EUR 57.85
XMR 0.017286084701815037
</p>
<h1>My name is Mogens, born on 29 May, 1958</h1>
這實際上是有道理的。
(如果有人有想法,我仍然希望能夠內聯javascript)
將script
標簽放在您的Pug代碼中會在已編譯的HTML中呈現script
標簽。 編譯時,它不會告訴Pug在script標記內執行任何javascript。 如果要在編譯代碼時在Pug中運行javascript,請使用未緩沖的代碼塊 。
-
// this is an unbuffered code block
// that will update the value of `trade.src_price`
// before it is rendered by Pug
if (trade.dst_currency === "EUR") {
trade.src_price = trade.dst_value / trade.src_value
} else {
trade.src_price = Number(trade.src_value) / Number(trade.dst_value)
}
table
thead
tr
th Currency
th Quantity
th Price
th Total
th Date
tbody
tr
th.align-middle #{trade.dst_currency}
th.align-middle #{trade.dst_value}
th.align-middle= #{trade.src_price}
th.align-middle #{trade.src_value} #{trade.src_currency}
th.align-middle #{trade.date}
if trade.name === "Bob"
h1 Hello Bob
else
h1 My name is #{trade.name}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.