Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- initTemplateHelpers () {
- //if helper
- handlebars.registerHelper('hif', (a, b, options) => {
- if (a == b) {
- return options.fn(this);
- } else {
- return options.inverse(this);
- }
- });
- //translation helper
- handlebars.registerHelper('locale', (domain, context) => {
- var phrase = typeof context === 'string' ? this.Locale[domain.toString()][context] : this.Locale[domain];
- if (!phrase) phrase = context;
- return new handlebars.SafeString(phrase);
- });
- //toLowercase helper
- handlebars.registerHelper('lower', (value) => {
- var string = value.toString().toLowerCase();
- if (string) {
- return new handlebars.SafeString(string);
- }
- })
- //for helper
- handlebars.registerHelper('for', function (from, to, isStrict, block) {
- var index = '';
- if (isStrict) {
- for (var i = from; i < to; ++i)
- index += block.fn(i);
- return index;
- }
- else {
- for (var i = from; i <= to; ++i)
- index += block.fn(i);
- return index;
- }
- });
- handlebars.registerHelper('xif', function (lvalue, operator, rvalue, options) {
- var operators, result;
- if (arguments.length < 3) {
- throw new Error("Handlerbars Helper 'compare' needs 2 parameters");
- }
- if (options === undefined) {
- options = rvalue;
- rvalue = operator;
- operator = "===";
- }
- operators = {
- '==' (l, r) { return l == r; },
- '===' (l, r) { return l === r; },
- '!=' (l, r) { return l != r; },
- '!==' (l, r) { return l !== r; },
- '<'(l, r) { return l < r; },
- '>' (l, r) { return l > r; },
- '<=' (l, r) { return l <= r; },
- '>=' (l, r) { return l >= r; },
- 'typeof' (l, r) { return typeof l == r; }
- };
- if (!operators[operator]) {
- throw new Error("'xIf' doesn't know the operator " + operator);
- }
- result = operators[operator](lvalue, rvalue);
- if (result) {
- return options.fn(this);
- } else {
- return options.inverse(this);
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement