Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

Example:

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)

switch(val) {
  case 1:
     return 'A';
     break;
  case 2:
     return 'B';
     break;
  default:
     return 'C';
}
like image 762
Sankar Subburaj Avatar asked Nov 20 '18 17:11

Sankar Subburaj


People also ask

Can you use JavaScript in handlebars?

Handlebars doesn't allow you to write JavaScript directly within templates. Instead, it gives you helpers. These are JavaScript functions that you can call from your templates, and help you reuse code and create complex templates. To call a helper, just use it as an expression - {{helpername}} .

How do you escape curly braces with handlebars?

Declare a template in the HTML file. Handlebars expressions are put into double curly braces {{expr}} for HTML-escaped content; otherwise, use triple curly brackets {{{expr}}} to avoid HTML-escaping.

What is helper in handlebars?

A Handlebars helper call is a simple identifier, followed by zero or more parameters (separated by a space). Each parameter is a Handlebars expression that is evaluated exactly the same way as described above in "Basic Usage": template {{firstname}} {{loud lastname}}

What are partials in handlebars?

Handlebars allows for template reuse through partials. Partials are normal Handlebars templates that may be called directly by other templates.


2 Answers

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.

Use the link http://tryhandlebarsjs.com/ to test the code. (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;
   }
});
like image 107
Sankar Subburaj Avatar answered Oct 03 '22 23:10

Sankar Subburaj


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.

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.

like image 37
Seoester Avatar answered Oct 04 '22 00:10

Seoester