如何处理Mustache模板中的IF STATEMENT? [英] How to handle an IF STATEMENT in a Mustache template?
问题描述
我正在使用小胡子。我正在生成通知列表。通知JSON对象如下所示:
I'm using mustache. I'm generating a list of notifications. A notification JSON object looks like:
[{"id":1364,"read":true,"author_id":30,"author_name":"Mr A","author_photo":"image.jpg","story":"wants to connect","notified_type":"Friendship","action":"create"}]
使用小胡子,我如何根据 informed_type <执行if语句或case语句/ code>&
行动
...
With mustache, how can I do a if statement or case statement based on the notified_type
& action
...
如果 informed_type ==友谊
渲染......
If notified_type == "Friendship"
render ......
如果 informed_type ==其他&& action ==邀请
渲染.....
这是如何工作的?
推荐答案
Mustache模板在设计上非常简单; 主页甚至说:
Mustache templates are, by design, very simple; the homepage even says:
无逻辑模板。
Logic-less templates.
所以一般方法是用JavaScript做你的逻辑并设置一堆标志:
So the general approach is to do your logic in JavaScript and set a bunch of flags:
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 :
If you want some more advanced functionality but want to maintain most of Mustache's simplicity, you could look at Handlebars:
Handlebars提供了必要的功能,让您可以有效地构建语义模板而不会受挫。
Handlebars provides the power necessary to let you build semantic templates effectively with no frustration.
小胡子模板与Handlebars兼容,因此您可以使用Mustache模板,将其导入Handlebars,并开始利用额外的Handlebars功能。
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.
这篇关于如何处理Mustache模板中的IF STATEMENT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!