-
Notifications
You must be signed in to change notification settings - Fork 8
/
if.js
30 lines (28 loc) · 1009 Bytes
/
if.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
'use strict';
/**
* overwrite default handlebars 'if' helper
* this adds support for an inline helper, `{{if foo bar}}` _(if foo is truthy, print bar)_
* as well as an inline if/else helper, `{{if foo bar else=baz}}` _(if foo is truthy, print bar. otherwise, print baz)_
* @param {*} conditional to check for truthiness
* @param {*} value to print if conditional is truthy
* @param {object} [options]
* @return {string} if inline, otherwise calls block functions
*/
module.exports = function (conditional, value, options) {
if (options === undefined) {
// only two arguments! behave like default handlebars if helper
options = value;
return conditional ? options.fn(this) : options.inverse(this);
} else {
// do the new inline behavior
if (conditional) {
return value;
} else {
return options.hash.else || ''; // return falsy value or emptystring
}
}
};
module.exports.example = {
code: '{{ if true "bar" else="baz" }}',
result: '"bar"'
};