我使用胡子我正在生成通知列表。通知JSON对象如下所示:
[{"id":1364,"read":true,"author_id":30,"author_name":"Mr A","author_photo":"image.jpg","story":"wants to connect","notified_type":"Friendship","action":"create"}]
有了胡子,我该怎么做一个if语句或case语句,基于notify_type&行动…
如果notify_type ==“友谊”呈现……
如果notify_type ==“其他&& action ==”邀请“render …..
如何工作?
解决方法
胡塞模板是按设计非常简单的;
homepage甚至说:
Logic-less templates.
所以一般的做法是在JavaScript中做你的逻辑,并设置一堆标志:
if(notified_type == "Friendship") data.type_friendship = true; else if(notified_type == "Other" && action == "invite") data.type_other_invite = true; //...
然后在你的模板中:
{{#type_friendship}} friendship... {{/type_friendship}} {{#type_other_invite}} invite... {{/type_other_invite}}
如果你想要一些更高级的功能,但是想保持大部分的Mustache的简单性,你可以看看Handlebars:
Handlebars provides the power necessary to let you build semantic templates effectively with no frustration.
Mustache templates are compatible with Handlebars,so you can take a Mustache template,import it into Handlebars,and start taking advantage of the extra Handlebars features.