[英]Pug/Jade and inline javascript calculations
I'm lost, again, trying to do some simple calculations in jade template. 我再次迷失了,试图在玉模板中做一些简单的计算。
Given this data object: 给定此数据对象:
{
"trade": {
"name": "Mogens",
"dst_currency": "EUR",
"dst_value": 115.7,
"src_price": null,
"src_value": 2,
"src_currency": "XMR",
"date": null
}
}
And this pug source: 和这个哈巴狗来源:
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}
How is this done, if at all possible? 如果有可能怎么办? What am I missing?
我想念什么?
Okay. 好的。 Figured it out at last.
最后想通了。
I had to give up on the inline script, and go with something more simple. 我不得不放弃内联脚本,而选择更简单的方法。
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}
Compiles to 编译为
<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>
And that actually makes some sense. 这实际上是有道理的。
(I would still like to be able to inline javascript, if anybody has ideas) (如果有人有想法,我仍然希望能够内联javascript)
Placing a script
tag in your Pug code renders a script
tag in your compiled HTML. 将
script
标签放在您的Pug代码中会在已编译的HTML中呈现script
标签。 It does not tell Pug to execute any javascript within the script tag when it compiles. 编译时,它不会告诉Pug在script标记内执行任何javascript。 If you want to run javascript in Pug as your code is compiled, use an unbuffered code block .
如果要在编译代码时在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.