1

我刚开始使用 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

但我想知道是否有更优雅的解决方案。

4

2 回答 2

0

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}}
于 2013-06-30T03:42:56.310 回答
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" };
    }
  }
});
于 2015-01-06T13:27:41.357 回答