Add "ifAny" Handlebars.js helper

This commit is contained in:
Dmitriy Simushev 2014-09-11 09:14:51 +00:00
parent accfbe0acd
commit 55a33cf012
2 changed files with 58 additions and 1 deletions

View File

@ -160,4 +160,37 @@
return options.inverse(this);
}
});
/**
* Registers "ifAny" helper.
*
* This helper checks if at least one argumet can be treated as
* "true" value. Example of usage:
* <code>
* {{#ifAny first second third}}
* At least one of argument can be threated as "true".
* {{else}}
* All values are "falsy"
* {{/ifAny}}
* </code>
*/
Handlebars.registerHelper('ifAny', function() {
var argsCount = arguments.length,
// The last helper's argument is the options hash. We need it to
// render the template.
options = arguments[argsCount - 1],
// All other helper's arguments are values that are used to evalute
// condition. Exctract that values from arguments pseudo array.
values = [].slice.call(arguments, 0, argsCount - 1);
for (var i = 0, l = values.length; i < l; i++) {
if (values[i]) {
// A true value is found. Render the positive block.
return options.fn(this);
}
}
// All values are "falsy". Render the negative block.
return options.inverse(this);
});
})(Mibew, Handlebars);

View File

@ -70,4 +70,28 @@ test('apply', function() {
'Test one valid helper and one unregistered helper'
);
}
});
});
// Test "ifAny" Handlebars helper
test('ifAny', function() {
var template = '{{#ifAny foo bar baz}}true{{else}}false{{/ifAny}}';
var compiledTemplate = Handlebars.compile(template);
equal(
compiledTemplate({}),
'false',
'Test only falsy values'
);
equal(
compiledTemplate({baz: true}),
'true',
'Test only one true value'
);
equal(
compiledTemplate({foo: true, bar: 1}),
'true',
'Test more than one true values'
);
});