handlebars添加条件判断

handlebars 的条件判断 {{#if}} 只能判空操作,但需要判断相等的时候,需要一个 helper 来帮助:

Handlebars.registerHelper('equal', function(v1, v2, options) {
if(v1 === v2) {
return options.fn(this);
}
return options.inverse(this);
});
使用的时候

{#equal v1 v2}}
{{v1}} is equal to {{v2}}
{{else}}
{{v1}} is not equal to {{v2}}
{{/equal}}

原文地址:https://www.cnblogs.com/cglWorkBook/p/5520682.html