gpt4 book ai didi

javascript - 在 Mustache.js 中使用逻辑

转载 作者:行者123 更新时间:2023-11-28 09:03:47 26 4
gpt4 key购买 nike

我刚刚开始使用 Mustache.js,遇到了一个问题。我知道 Mustache.js 是无逻辑的(就像handlebars.js 一样),但是可以使用一些逻辑(true/false),所以我想知道是否可以实现以下目标。这是我的情况:

6 个 HTML 模板,除以下内容外均完全相同:

  • 艺术家
  • 日期
  • 地点
  • 网址
  • 促销类型:(预售 | 周五促销 | 周六促销 | 周日促销 | 立即促销 | 特别优惠)

我可以使用 mustache 轻松处理此内容,因为销售是预售或特价。但是,我想根据销售类型轮换一张图像。示例:

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

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

最佳答案

这是我最终决定的解决方案:

{ 
"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}}

关于javascript - 在 Mustache.js 中使用逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17375506/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com