简体   繁体   English

如何在 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?那不可能的情况呢?

This is how you do if/else in Mustache (perfectly supported):这就是您在 Mustache 中执行 if/else 的方式(完全支持):

{{#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#inverted-sections在文档中查找倒置部分: https : //github.com/janl/mustache.js#inverted-sections

This is something you solve in the "controller", which is the point of logicless templating.这是您在“控制器”中解决的问题,这是无逻辑模板的重点。

// some function that retreived data through ajax
function( view ){

   if ( !view.avatar ) {
      // DEFAULTS can be a global settings object you define elsewhere
      // so that you don't have to maintain these values all over the place
      // in your code.
      view.avatar = DEFAULTS.AVATAR;
   }

   // do template stuff here

}

This is actually a LOT better then maintaining image url's or other media that might or might not change in your templates, but takes some getting used to.这实际上比维护图像 url 或其他可能会或可能不会在您的模板中更改的媒体要好得多,但需要一些时间来适应。 The point is to unlearn template tunnel vision, an avatar img url is bound to be used in other templates, are you going to maintain that url on X templates or a single DEFAULTS settings object?关键是要忘记模板隧道视觉,头像 img url 必然会在其他模板中使用,您是要在 X 模板上维护该 url 还是单个 DEFAULTS 设置对象? ;) ;)

Another option is to do the following:另一种选择是执行以下操作:

// augment view
view.hasAvatar = !!view.avatar;
view.noAvatar = !view.avatar;

And in the template:在模板中:

{{#hasAvatar}}
    SHOW AVATAR
{{/hasAvatar}}
{{#noAvatar}}
    SHOW DEFAULT
{{/noAvatar}}

But that's going against the whole meaning of logicless templating.但这违背了无逻辑模板的全部含义。 If that's what you want to do, you want logical templating and you should not use Mustache, though do give it yourself a fair chance of learning this concept ;)如果这就是你想要做的,你想要逻辑模板,你不应该使用 Mustache,但一定要给自己一个学习这个概念的公平机会;)

Your else statement should look like this (note the ^ ):您的else语句应如下所示(注意^ ):

{{^avatar}}
 ...
{{/avatar}}

In mustache this is called 'Inverted sections'.在小胡子中,这称为“倒置部分”。

Note, you can use {{.}} to render the current context item.请注意,您可以使用{{.}}来呈现当前上下文项。

{{#avatar}}{{.}}{{/avatar}}

{{^avatar}}missing{{/avatar}}

You can define a helper in the view.您可以在视图中定义一个助手。 However, the conditional logic is somewhat limited.但是,条件逻辑有些限制。 Moxy-Stencil ( https://github.com/dcmox/moxyscript-stencil ) seems to address this with "parameterized" helpers, eg: Moxy-Stencil ( https://github.com/dcmox/moxyscript-stencil ) 似乎通过“参数化”助手解决了这个问题,例如:

{{isActive param}} {{isActive 参数}}

and in the view:并在视图中:

view.isActive = function (path: string){ return path === this.path ? view.isActive = function (path: string){ return path === this.path ? "class='active'" : '' } "class='active'" : '' }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM