我在 Jade 的条件块中设置变量。我有许多对象具有两种不同类型的评级中的零个、一个或两个。每个对象有两个<span>
标签。如果评级不存在,则跨度标签留空。否则,我将适当的评级插入到我的跨度标签中。我需要这些变量以供将来在 DOM 中进一步使用。
您可以在这里试验以下 Jade 代码:http:
//cssdeck.com/labs/a5rru5vj
编辑:下面的简单解决方案,留下这个以防它帮助其他人:D
//Jade
- var demo = [{rating1:'2.5'}, {rating2:3.333333}]
each obj in demo
if(obj.rating1 !== undefined)
- var r1 = obj.rating1 || ""
= r1
else
- var r1 = ""
= r1
if(obj.rating2 !== undefined)
- var r2 = String(obj.rating2).slice(0,3)
= r2
else
- var r2 = ""
= r2
div.objIndex(data-obj=JSON.stringify(obj))
span.myRating= r1
span.googleRating= r2
// output html (with whitespace cleaned up)
// Note that Jade is writing the variables into the output, why, and how to avoid this?
<body>
2.5
<div data-obj="{"rating1":"2.5"}" class="objIndex">
<span class="myRating">
2.5
</span>
<span class="googleRating">
</span>
</div>
3.3
<div data-obj="{"rating2":3.333333}" class="objIndex">
<span class="myRating">
</span>
<span class="googleRating">
3.3
</span>
</div>
</body>