I would like to know if it is possible to use the “Switch” condition in template7. I did a search in the documentation but found no information for this condition …
Its possible ?
{{#switch}}
{{case 1}}
{{case 2}}
{{/switch}}
I would like to know if it is possible to use the “Switch” condition in template7. I did a search in the documentation but found no information for this condition …
Its possible ?
{{#switch}}
{{case 1}}
{{case 2}}
{{/switch}}
No, such is not not possible
Try to register your own helpers, I’ve borrowed them from handlebars.
Template7.registerHelper("switch", function (value, options) {
this._switch_value_ = value;
let html = options.fn(this); // Process the body of the switch block
delete this._switch_value_;
return html;
});
Template7.registerHelper("case", function () {
let args = Array.prototype.slice.call(arguments);
let options = args.pop();
let caseValues = args;
if (caseValues.indexOf(this._switch_value_) === -1) {
return '';
} else {
return options.fn(this);
}
});
after use them in this way:
{{#switch url}}
{{#case '/' '/home'}}
first case
{{/case}}
{{#case '/about'}}
second case
{{/case}}
{{/switch}}
Here another modified with the default case
Template7.registerHelper('switch', function (value, options) {
this._switch_value_ = value;
this._switch_break_ = false;
let html = options.fn(this);
delete this._switch_break_;
delete this._switch_value_;
return html;
});
Template7.registerHelper('case', function (value, options) {
let args = Array.prototype.slice.call(arguments);
options = args.pop();
let caseValues = args;
if (this._switch_break_ || caseValues.indexOf(this._switch_value_) === -1) {
return '';
} else {
this._switch_break_ = true;
return options.fn(this);
}
});
Template7.registerHelper('default', function (options) {
if (!this._switch_break_) {
return options.fn(this);
}
});
Usage is simple:
{{#switch url}}
{{#case '/' '/home'}} <!--url for both of '/' and '/home' -->
first case
{{/case}}
{{#case '/about'}}
second case
{{/case}}
{{#default}}
DEFAULT CASE
{{/default}}
{{/switch}}
fixed error returning ‘’ instead of undefined;
Template7.registerHelper('default', function (options) {
if (!this._switch_break_) {
return options.fn(this);
} else {
return '';
}
});
Thanks Almazk!!! Its Great!!!
Thanx a lot it is work !!