Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. initTemplateHelpers () {
  2. //if helper
  3. handlebars.registerHelper('hif', (a, b, options) => {
  4. if (a == b) {
  5. return options.fn(this);
  6. } else {
  7. return options.inverse(this);
  8. }
  9. });
  10.  
  11. //translation helper
  12. handlebars.registerHelper('locale', (domain, context) => {
  13. var phrase = typeof context === 'string' ? this.Locale[domain.toString()][context] : this.Locale[domain];
  14. if (!phrase) phrase = context;
  15. return new handlebars.SafeString(phrase);
  16. });
  17.  
  18. //toLowercase helper
  19. handlebars.registerHelper('lower', (value) => {
  20. var string = value.toString().toLowerCase();
  21. if (string) {
  22. return new handlebars.SafeString(string);
  23. }
  24. })
  25.  
  26. //for helper
  27. handlebars.registerHelper('for', function (from, to, isStrict, block) {
  28. var index = '';
  29. if (isStrict) {
  30. for (var i = from; i < to; ++i)
  31. index += block.fn(i);
  32. return index;
  33. }
  34. else {
  35. for (var i = from; i <= to; ++i)
  36. index += block.fn(i);
  37. return index;
  38. }
  39.  
  40. });
  41. handlebars.registerHelper('xif', function (lvalue, operator, rvalue, options) {
  42.  
  43. var operators, result;
  44.  
  45. if (arguments.length < 3) {
  46. throw new Error("Handlerbars Helper 'compare' needs 2 parameters");
  47. }
  48.  
  49. if (options === undefined) {
  50. options = rvalue;
  51. rvalue = operator;
  52. operator = "===";
  53. }
  54.  
  55. operators = {
  56. '==' (l, r) { return l == r; },
  57. '===' (l, r) { return l === r; },
  58. '!=' (l, r) { return l != r; },
  59. '!==' (l, r) { return l !== r; },
  60. '<'(l, r) { return l < r; },
  61. '>' (l, r) { return l > r; },
  62. '<=' (l, r) { return l <= r; },
  63. '>=' (l, r) { return l >= r; },
  64. 'typeof' (l, r) { return typeof l == r; }
  65. };
  66.  
  67. if (!operators[operator]) {
  68. throw new Error("'xIf' doesn't know the operator " + operator);
  69. }
  70.  
  71. result = operators[operator](lvalue, rvalue);
  72.  
  73. if (result) {
  74. return options.fn(this);
  75. } else {
  76. return options.inverse(this);
  77. }
  78. });
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement