简体   繁体   English

在 handlebars.js 中使用默认开关大小写

[英]Switch case with default in handlebars.js

I want to implement a custom switch case with default value with the help of the Register Helper function in HandlebarsJs.我想借助 HandlebarsJs 中的 Register Helper function 实现具有default值的自定义switch case

Example:例子:

HTML: HTML:

<div>
    {{#switch value}} 
        {{#case 'a'}} A {{/case}} 
        {{#case 'b'}} B {{/case}} 
        {{#default 'C'}} {{/default}}
    {{/switch}}
</div>

JS:记者: (The Register Helper function should work like the below pseudo code) (注册助手 function 应该像下面的伪代码一样工作)

switch(val) {
  case 1:
     return 'A';
     break;
  case 2:
     return 'B';
     break;
  default:
     return 'C';
}

Please go through the below examples, it will guide you step by step to add the switch case with default value and break in HandlebarsJs.请仔细阅读以下示例,它将逐步指导您在 HandlebarsJs 中添加具有默认值的 switch case 和 break。

Use the link http://tryhandlebarsjs.com/ to test the code.使用链接http://tryhandlebarsjs.com/测试代码。 (Give {} for Context) (为上下文提供{}

Switch case开关盒

Handlebars Template:车把模板:

<div>

        {{#switch 'a'}} 
            {{#case 'a'}} A {{/case}} 
            {{#case 'b'}} B {{/case}} 
        {{/switch}}

</div>

Register Helper functions:注册助手功能:

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

========================================================================== ================================================== ========================

Switch case with default:默认情况下切换案例:

Handlebars Template:车把模板:

<div>

        {{#switch 'a'}} 
            {{#case 'a'}} A {{/case}} 
            {{#case 'b'}} B {{/case}}
            {{#default '188'}} {{/default}}
        {{/switch}}

</div>

Register Helper functions:注册助手功能:

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

Handlebars.registerHelper('default', function(value, options) {
    return true; ///We can add condition if needs
});

========================================================================== ================================================== ========================

Switch case with default and break使用默认和中断切换案例

Handlebars Template:车把模板:

<div>
        {{#switch 'a'}} 
            {{#case 'a'}} A {{/case}} 
            {{#case 'b'}} B {{/case}} 
            {{#default '188'}} {{/default}}
        {{/switch}}
</div>

Register Helper functions:注册助手功能:

Handlebars.registerHelper('switch', function(value, options) {
  this.switch_value = value;
  this.switch_break = false;
  return options.fn(this);
});

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

Handlebars.registerHelper('default', function(value, options) {
   if (this.switch_break == false) {
     return value;
   }
});

I want to use #switch with #case and #default without setting a value for #default so I customize a popular answer.我想在不为#default 设置值的情况下将#switch 与#case 和#default 一起使用,因此我自定义了一个流行的答案。

Switch case with default and break (via case -> default)使用默认和中断切换大小写(通过大小写->默认)

Handlebars Template:车把模板:

<div>
        {{#switch myvar}} 
            {{#case 'a'}} A {{/case}} 
            {{#case 'b'}} B {{/case}}
            {{#case 'default'}} Default output {{/case}}
        {{/switch}}

</div>

Register Helper functions:注册助手功能:

Handlebars.registerHelper('switch', function(value, options) {
  this.switch_value = value;
  this.switch_break = false;
  return options.fn(this);
});

Handlebars.registerHelper('case', function(value, options) {
  if (value == this.switch_value || (value == 'default' && this.switch_break == false)) {
     this.switch_break = true;
     return options.fn(this);
  }
});

In this solution you have to use {{#case 'default'} } instead of {{#default '12345'}} and the helper prints the content of #case, just like the others.在此解决方案中,您必须使用{{#case 'default'} } 而不是 {{#default '12345'}}并且 helper 打印 #case 的内容,就像其他人一样。

One helper less and a little more complexity in #case helper. #case helper 中少了一个 helper 并且复杂了一点。

This works very fine for me.这对我来说很好用。

Pay attention: put {{#case 'default'}} at the end of switch, and obviously the value 'default' cannot be in range of myvar.注意:把{{#case 'default'}}放在switch的末尾,显然'default'的值不能在myvar的范围内。

Just if you want to convert key to value.就像您想将键转换为值一样。

module.exports = function(input, cases, values) {
  const caseArray = cases.split(',')
  const valueArray = values.split(',')
  const defaultValue = caseArray.length < valueArray.length ? valueArray[valueArray.length-1] : "";

  return caseArray.indexOf(input) > -1? valueArray[caseArray.indexOf(input)] : defaultValue 
};
{{switch "Y" "Y,N,D", "YES,NO,DELETED,-"}}  // YES
{{switch "Hi" "Y,N,D", "YES,NO,DELETED,-"}}  // -

If all you need is an if - else if - else structure, consider to use the built-in if / else helpers (see Helpers: Conditionals on "chained" conditionals) and define only the comparison operation in your own helper.如果您只需要一个if - else if - else结构,请考虑使用内置的if / else助手(请参阅 助手: “链式”条件上的条件)并仅在您自己的助手中定义比较操作。

Helper帮手

Handlebars.registerHelper('isEqual', (value1, value2, options) => {
  return value1 === value2;
});

Template模板

<div>
    {{#if (isEqual value 'a')}} 
        A
    {{else if (isEqual value 'b')}}
        B
    {{else}}
        C
    {{/if}}
</div>

Initially answered here .最初在这里回答。

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

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