handlebars 只有提供簡單的 true, false判斷
如果要判斷某個值大於多少
就要另外寫extension了
在stackoverflow上已經有人寫好完整的extension
所有比較都在裡面了
Handlebars.registerHelper('ifCond', function (v1, operator, v2, options) {
switch (operator) {
case '==':
return (v1 == v2) ? options.fn(this) : options.inverse(this);
case '===':
return (v1 === v2) ? options.fn(this) : options.inverse(this);
case '!=':
return (v1 != v2) ? options.fn(this) : options.inverse(this);
case '!==':
return (v1 !== v2) ? options.fn(this) : options.inverse(this);
case '<':
return (v1 < v2) ? options.fn(this) : options.inverse(this);
case '<=':
return (v1 <= v2) ? options.fn(this) : options.inverse(this);
case '>':
return (v1 > v2) ? options.fn(this) : options.inverse(this);
case '>=':
return (v1 >= v2) ? options.fn(this) : options.inverse(this);
case '&&':
return (v1 && v2) ? options.fn(this) : options.inverse(this);
case '''':
return (v1 '' v2) ? options.fn(this) : options.inverse(this);
default:
return options.inverse(this);
}
});
加入extension之後就可以在template裡面使用
類似這樣的 code
{{#ifCond var1 '==' var2}}