Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
177 views
in Technique[技术] by (71.8m points)

javascript - 如何在mustache.js中完成if / else?(How do I accomplish an if/else in mustache.js?)

It seems rather odd that I can't figure how to do this in mustache.(似乎很奇怪,我无法想象如何在胡子中做到这一点。)

Is it supported?(是否支持?) This is my sad attempt at trying:(这是我尝试的悲伤尝试:) {{#author}} {{#avatar}} <img src="{{avatar}}"/> {{/avatar}} {{#!avatar}} <img src="/images/default_avatar.png" height="75" width="75" /> {{/avatar}} {{/author}} This obviously isn't right, but the documentation doesn't mention anything like this.(这显然是不对的,但文档中没有提到这样的事情。) The word "else" isn't even mentioned :((甚至没有提到“其他”这个词:() Also, why is mustache designed this way?(另外,为什么胡子是这样设计的?) Is this sort of thing considered bad?(这种事情被认为是坏事吗?) Is it trying to force me to set the default value in the model itself?(它是否试图强迫我在模型中设置默认值?) What about the cases where that isn't possible?(那些不可能的情况怎么样?)   ask by egervari translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is how you do if/else in Mustache (perfectly supported):(这就是你在Mustache中做/ if的方法(完全支持):)

{{#repo}} <b>{{name}}</b> {{/repo}} {{^repo}} No repos :( {{/repo}} Or in your case:(或者在你的情况下:) {{#author}} {{#avatar}} <img src="{{avatar}}"/> {{/avatar}} {{^avatar}} <img src="/images/default_avatar.png" height="75" width="75" /> {{/avatar}} {{/author}} Look for inverted sections in the docs: https://github.com/janl/mustache.js(在文档中查找倒置部分: https//github.com/janl/mustache.js)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...