微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

jquery – 如何处理一个谚语模板中的IF声明?

我使用胡子我正在生成通知列表。通知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.

原文地址:https://www.jb51.cc/jquery/183286.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐