如何在 Mustache.js 中完成 if/else?

发布于 2024-11-07 16:31:59 字数 420 浏览 0 评论 0原文

我不知道如何在小胡子中做到这一点,这似乎很奇怪。支持吗?

这是我悲伤的尝试:

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

这显然是不对的,但文档没有提到这样的事情。甚至没有提到“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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

双马尾 2024-11-14 16:31:59

这就是您在 Mustache 中执行 if/else 的方式(完全支持):

{{#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}}

在文档中查找倒置部分:

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

{{#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

枫林﹌晚霞¤ 2024-11-14 16:31:59

这是您在“控制器”中解决的问题,这就是无逻辑模板的要点。

// 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

}

这实际上比维护模板中可能会或可能不会改变的图像 URL 或其他媒体要好得多,但需要一些时间来适应。重点是要忘记模板隧道视觉,头像 img url 必然会在其他模板中使用,您是否要在 X 模板或单个 DEFAULTS 设置对象上维护该 url? ;)

另一种选择是执行以下操作:

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

在模板中:

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

但这违背了无逻辑模板的全部含义。如果这就是您想要做的,那么您需要逻辑模板并且您不应该使用 Mustache,但请给自己一个学习这个概念的公平机会;)

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. 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? ;)

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 ;)

诠释孤独 2024-11-14 16:31:59

您的 else 语句应如下所示(请注意 ^):

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

在 Mustache 中,这称为“反向部分”。

Your else statement should look like this (note the ^):

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

In mustache this is called 'Inverted sections'.

余厌 2024-11-14 16:31:59

请注意,您可以使用 {{.}} 来呈现当前上下文项。

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

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

Note, you can use {{.}} to render the current context item.

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

{{^avatar}}missing{{/avatar}}
注定孤独终老 2024-11-14 16:31:59

您可以在视图中定义一个助手。然而,条件逻辑有些有限。 Moxy-Stencil (https://github.com/dcmox/moxyscript-stencil) 似乎使用“参数化”帮助器解决这个问题,例如:

{{isActive param}}

并在视图中:

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

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:

{{isActive param}}

and in the view:

view.isActive = function (path: string){ return path === this.path ? "class='active'" : '' }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文