Created
December 11, 2013 12:32
-
-
Save adityapunjani/7909622 to your computer and use it in GitHub Desktop.
JS Helpers to use with Handlebars.java client.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Handlebars.registerHelper('if_mod', function(num, val, result, options) { | |
if ((num % val) === result) { | |
return options.fn(this); | |
} else return options.inverse(this); | |
}); | |
Handlebars.registerHelper('toJSON', function(obj) { | |
return JSON.stringify(obj); | |
}); | |
Handlebars.registerHelper('if_notMod', function(num, val, result, options) { | |
if ((num % val) !== result) { | |
return options.fn(this); | |
} | |
}); | |
Handlebars.registerHelper('subtract', function(lval, rval) { | |
return (lval - rval); | |
}); | |
Handlebars.registerHelper('if_gt', function(lval, rval, options) { | |
lval = parseInt(lval, 10); | |
rval = parseInt(rval, 10); | |
if (lval > rval) { | |
return options.fn(this); | |
} else return options.inverse(this); | |
}); | |
Handlebars.registerHelper('toUpperCase', function(string) { | |
return string.toUpperCase(); | |
}); | |
//TODO::Aditya: get rid of this, make it generic | |
Handlebars.registerHelper('geticmpid', function(pagetype, pos, id) { | |
var icmpid = pagetype + "_" + pos + "_" + id; | |
return icmpid; | |
}); | |
Handlebars.registerHelper('if_eq', function(lvalue, rvalue, options) { | |
if (arguments.length < 3) | |
throw new Error("Handlebars Helper equal needs 2 parameters"); | |
if (lvalue != rvalue) { | |
return options.inverse(this); | |
} else { | |
return options.fn(this); | |
} | |
}); | |
Handlebars.registerHelper('if_str_eq', function(lvalue, rvalue, options) { | |
if (arguments.length < 3) | |
throw new Error("Handlebars Helper equal needs 2 parameters"); | |
lvalue = lvalue.toUpperCase(); | |
rvalue = rvalue.toUpperCase(); | |
if (lvalue != rvalue) { | |
return options.inverse(this); | |
} else { | |
return options.fn(this); | |
} | |
}); | |
Handlebars.registerHelper('if_notEq', function(lvalue, rvalue, options) { | |
if (arguments.length < 3) | |
throw new Error("Handlebars Helper equal needs 2 parameters"); | |
if (lvalue == rvalue) { | |
return options.inverse(this); | |
} else { | |
return options.fn(this); | |
} | |
}); | |
Handlebars.registerHelper('if_str_notEq', function(lvalue, rvalue, options) { | |
if (arguments.length < 3) | |
throw new Error("Handlebars Helper equal needs 2 parameters"); | |
lvalue = lvalue.toUpperCase(); | |
rvalue = rvalue.toUpperCase(); | |
if (lvalue == rvalue) { | |
return options.inverse(this); | |
} else { | |
return options.fn(this); | |
} | |
}); | |
Handlebars.registerHelper('if_all', function if_all(x, y, z, options) { | |
var args = Array.prototype.slice.call(arguments); | |
var i; | |
if (if_all.length !== arguments.length) { | |
options = args[args.length - 1]; | |
} | |
for (i = 0; i < args.length; i++) { | |
if (args[i] === "" || args[i] === null || args[i] === 0) { | |
return options.inverse(this); | |
} | |
} | |
return options.fn(this); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment