首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >车把模拟开关(外壳)

车把模拟开关(外壳)
EN

Stack Overflow用户
提问于 2019-04-09 19:54:04
回答 3查看 1.2K关注 0票数 0

我想要模拟这样的东西:

代码语言:javascript
复制
{{#each this}}
   {{if @index == 0}}
      <li><img src="/Content/totem/images/0.png" alt="" height="200" width="200" /></li>
   {{else @index == 1}}
      <li><img src="/Content/totem/images/1.png" alt="" height="200" width="200" /></li>
   {{else if}}
      <li><img src="/Content/totem/images/2.png" alt="" height="200" width="200" /></li>
{{/each}}

这有可能吗?

EN

回答 3

Stack Overflow用户

发布于 2019-04-09 19:58:35

是的,一旦添加了相关的帮助器函数,就可以使用下面描述的语法here

帮助器函数

代码语言:javascript
复制
Handlebars.registerHelper('switch', function(value, options) {
  this.switch_value = value;
  return options.fn(this);
});

Handlebars.registerHelper('case', function(value, options) {
  if (value == this.switch_value) {
    return options.fn(this);
  }
});

交换机

代码语言:javascript
复制
{{#switch this}} 
   {{#case 0}}
      <li><img src="/Content/totem/images/0.png" alt="" height="200" width="200" /></li>
   {{/case}}
   {{#case 1}}
      <li><img src="/Content/totem/images/1.png" alt="" height="200" width="200" /></li>
   {{/case}}
   {{#case 2}}
      <li><img src="/Content/totem/images/2.png" alt="" height="200" width="200" /></li>
   {{/case}}
{{/switch}}
票数 1
EN

Stack Overflow用户

发布于 2021-06-23 06:33:44

您需要一个帮助器来进行比较。这与Mustache提出的无逻辑模板语言的前提背道而驰。然而,它是实用的。

我喜欢使用内置的if/else帮助器(参见文档中的Helpers: Conditionals ),而不是定义切换用例,并且只将比较操作定义为帮助器。

辅助对象

代码语言:javascript
复制
Handlebars.registerHelper('isEqual', (value1, value2, options) => {
  return value1 === value2;
});

模板

代码语言:javascript
复制
{{#each this}}
   {{#if (isEqual @index 0)}}
      <li><img src="/Content/totem/images/0.png" alt="" height="200" width="200" /></li>
   {{else if (isEqual @index 1)}}
      <li><img src="/Content/totem/images/1.png" alt="" height="200" width="200" /></li>
   {{else}}
      <li><img src="/Content/totem/images/2.png" alt="" height="200" width="200" /></li>
   {{/if}}
{{/each}}
票数 1
EN

Stack Overflow用户

发布于 2021-07-25 16:18:09

请参阅this注释

代码语言:javascript
复制
{{> (lookup . state) }}
{{#*inline "page1"}}page 1{{/inline}}
{{#*inline "page2"}}page 2{{/inline}}
{{#*inline "undefined"}}page 2{{/inline}}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55592260

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档