[英]using logic in mustache.js
I just started working with mustache.js and I came across a problem. 我刚开始使用mustache.js,遇到一个问题。 I understand that mustache.js is logic-less (as is handlebars.js) but there is some logic (true/false) that can be used, so I'm wondering if the following can be achieved.
我知道mustache.js的逻辑较少(如handlebars.js一样),但是可以使用某些逻辑(真/假),所以我想知道是否可以实现以下目标。 Here's my situation:
这是我的情况:
6 HTML templates, all exactly the same except for the following: 6个HTML模板,除了以下内容外,其余完全相同:
I can easily work with this content using mustache, as the sale is EITHER presale OR on sale. 我可以轻松地使用小胡子处理此内容,因为这笔交易是预售还是出售。 However, I have an image that I want to rotate through based on the type of sale.
但是,我有一张要根据销售类型轮流浏览的图片。 Example:
例:
json: 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
}
template: 模板:
<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>
my problem is how to implement one of 6 images, 我的问题是如何实现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}}
the easy way to do it would be: 最简单的方法是:
"imgPresale": true,
"imgOnSaleNow": false,
"imgOnSaleFriday": false,
"imgOnSaleSaturday": false,
"imgOnSaleSunday": false,
"imgSpecialOffer": false
but I'm wondering if there is a more elegant solution. 但我想知道是否有更优雅的解决方案。
Here's the solution I finally decided on: 这是我最终决定采用的解决方案:
{
"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
}
}
}
template: 模板:
{{#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}}
Remember that you can also use functions as the data you pass to your template, for example: 请记住,您还可以将函数用作传递给模板的数据,例如:
{{#sale}}
{{#img}}
<img src="{{src}}" width="225" height="45" alt="{{desc}}">
{{/img}}
{{/sale}}
And then: 接着:
Mustache.render(template,{
sale: {
img: function() {
// Put logic here and return what you want:
return { src:"presale.jpg", desc:"Exclusive Presale Offer" };
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.