我刚开始使用mustache.js,我遇到了一个问题。我知道mustache.js是无逻辑的(就像handlebars.js一样)但是有一些逻辑(真/假)可以使用,所以我想知道是否可以实现以下。这是我的情况:
6个HTML模板,除以下内容外完全相同:
我可以使用小胡子轻松处理这些内容,因为销售是预售或出售。但是,根据销售类型,我有一个想要旋转的图像。例如:
JSON:
{
"artist": "John Doe",
"date": 1026,
"venue": "Civic Center",
"offer": {
"text": "Exclusive Presale offer",
"presale": "Thursday 10AM-10PM",
"password": "PRESALE",
"public": "Friday 10AM"
},
"url": "http://www.buy.com",
"presale": true,
"onSale": false
}
模板:
<font face="Verdana, Arial, Helvetica, sans-serif" size="-2" color="#b7b7b7">
{{#presale}}
Presale Offer for {{artist}}, {{offer.presale}}, use password {{offer.password}}.
{{/presale}}
{{#onSale}}
{{artist}} at {{venue}} on {{date}}, tickets are on sale {{offer.public}}.
{{/onSale}}
View this message in a <a href="{{view_email_url}}" alias="chtv link"><font size="-2" face="Verdana, Arial, Helvetica, sans-serif" color="#b7b7b7">browser</font></a>.</font>
我的问题是如何实现6个图像中的一个,
{{#imgPresale}}
<img src="presale.jpg" width="225" height="45" alt="Exclusive Presale Offer" style="display:block">
{{/imgPresale}}
{{#imgOnSaleFriday}}
<img src="osFri.jpg" width="225" height="45" alt="On Sale Friday" style="display:block">
{{/imgOnSaleFriday}}
{{#imgOnSaleSaturday}}
<img src="osSa.jpg" width="225" height="45" alt="On Sale Saturday" style="display:block">
{{/imgOnSaleSaturday}}
{{#onSaleSunday}}
<img src="osSu.jpg" width="225" height="45" alt="On Sale Sunday" style="display:block">
{{/imgOnSaleSunday}}
{{#imgOnSaleNow}}
<img src="osn.jpg" width="225" height="45" alt="On Sale Now" style="display:block">
{{/imgOnSaleNow}}
{{#imgSpecialOffer}}
<img src="offer.jpg" width="225" height="45" alt="Special Offer" style="display:block">
{{/imgSpecialOffer}}
这样做的简单方法是:
"imgPresale": true,
"imgOnSaleNow": false,
"imgOnSaleFriday": false,
"imgOnSaleSaturday": false,
"imgOnSaleSunday": false,
"imgSpecialOffer": false
但我想知道是否有更优雅的解决方案。
答案 0 :(得分:0)
这是我最终决定的解决方案:
{
"artist": "John Doe",
"support": false,
"date": "1026",
"venue": "Civic Center",
"url": "http://buy.com",
"offer": {
"text": "Exclusive Presale offer",
"presale": "Thursday 10AM-10PM",
"password": "PRESALE",
"public": "Friday 10AM"
},
"sale" : {
"presale" : true,
"specialOffer" : false,
"onSale": {
"Now": false,
"Friday": false,
"Saturday": false,
"Sunday": false
}
}
}
模板:
{{#sale}}
{{#sale.presale}}
<img src="presale.jpg" width="225" height="45" alt="Exclusive Presale Offer" style="display:block">
{{/sale.presale}}
{{#sale.specialOffer}}
<img src="offer.jpg" width="225" height="45" alt="Special Offer" style="display:block">
{{/sale.specialOffer}}
<!-- - - - - - - - - - - - - - - - - -->
{{#sale.onSale.Now}}
<img src="osn.jpg" width="225" height="45" alt="On Sale Now" style="display:block">
{{/sale.onSale.Now}}
{{#sale.onSale.Friday}}
<img src="osFri.jpg" width="225" height="45" alt="On Sale Friday" style="display:block">
{{/sale.onSale.Friday}}
{{#sale.onSale.Saturday}}
<img src="osSa.jpg" width="225" height="45" alt="On Sale Saturday" style="display:block">
{{/sale.onSale.Saturday}}
{{#sale.onSale.Sunday}}
<img src="osSu.jpg" width="225" height="45" alt="On Sale Sunday" style="display:block">
{{/sale.onSale.Sunday}}
{{/sale}}
<!-- - - - - - - - - - - - - - - - - -->
{{^sale}}
<img src="blank.jpg" width="225" height="45" style="display:block">
{{/sale}}
答案 1 :(得分:0)
请记住,您还可以将函数用作传递给模板的数据,例如:
{{#sale}}
{{#img}}
<img src="{{src}}" width="225" height="45" alt="{{desc}}">
{{/img}}
{{/sale}}
然后:
Mustache.render(template,{
sale: {
img: function() {
// Put logic here and return what you want:
return { src:"presale.jpg", desc:"Exclusive Presale Offer" };
}
}
});