mirror of
https://github.com/Mibew/mibew.git
synced 2025-03-02 18:08:32 +03:00
Add "ifAny" Handlebars.js helper
This commit is contained in:
parent
accfbe0acd
commit
55a33cf012
@ -160,4 +160,37 @@
|
|||||||
return options.inverse(this);
|
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);
|
})(Mibew, Handlebars);
|
@ -71,3 +71,27 @@ test('apply', function() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 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'
|
||||||
|
);
|
||||||
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user