如何在mustache.js中完成if / else? [英] How do I accomplish an if/else in mustache.js?

查看:1914
本文介绍了如何在mustache.js中完成if / else?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎很奇怪,我无法想象如何在胡子中做到这一点。是否支持?

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?

推荐答案

这就是你的方法在Mustache中做if / else(完全支持):

This is how you do if/else in Mustache (perfectly supported):

{{#repo}}
  <b>{{name}}</b>
{{/repo}}
{{^repo}}
  No repos :(
{{/repo}}

或者在您的情况下:

{{#author}}
  {{#avatar}}
    <img src="{{avatar}}"/>
  {{/avatar}}
  {{^avatar}}
    <img src="/images/default_avatar.png" height="75" width="75" />
  {{/avatar}}
{{/author}}

查找文档中的倒置部分: https://github.com/janl/mustache.js

Look for inverted sections in the docs: https://github.com/janl/mustache.js

这篇关于如何在mustache.js中完成if / else?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆