Skip to content

Instantly share code, notes, and snippets.

@elmeunick9
Last active April 11, 2023 10:45
Show Gist options
  • Save elmeunick9/5ecd083075fba46a2e890b5a8a083e4a to your computer and use it in GitHub Desktop.
Save elmeunick9/5ecd083075fba46a2e890b5a8a083e4a to your computer and use it in GitHub Desktop.
Get logic into your templates
// Usage: {{#if (isdefined x)}} ... {{/if}}
Handlebars.registerHelper('isdefined', function (value) {
return value !== undefined;
})
// Read as: Equals
// Usage: {{#if (eq x y)}} ... x == y ... {{/if}}
// Usage: {{#if (eq 1 x y z)}} ... 1 == x && x == y && y == z [x, y and z ara all 1] ... {{/if}}
Handlebars.registerHelper('eq', function (...args) {
return args.slice(0, -1).map((x: any, i: number, v: any[]) => x == v[i+1]).slice(0, -1).every(x => x)
})
// Read as: Not Equals
// Usage: {{#if (neq x y)}} ... x != y ... {{/if}}
// Usage: {{#if (neq x 2 x 3 x 5 x 7 x 11 x 13 x 17 x 19)}} ... x != 2 && x != 3 && x != 5 && [...] ... {{/if}}
Handlebars.registerHelper('neq', function (...args) {
return args.slice(0, -1).map((x: any, i: number, v: any[]) => x != v[i+1]).slice(0, -1).every(x => x)
})
// Read as: Greather Than
// Usage: {{#if (gt x y)}} ... x > y ... {{/if}}
Handlebars.registerHelper('gt', function (...args) {
return args.slice(0, -1).map((x: any, i: number, v: any[]) => x > v[i+1]).slice(0, -1).every(x => x)
})
// Read as: Greather Than or Equal
// Usage: {{#if (gte x y)}} ... x >= y ... {{/if}}
Handlebars.registerHelper('gte', function (...args) {
return args.slice(0, -1).map((x: any, i: number, v: any[]) => x >= v[i+1]).slice(0, -1).every(x => x)
})
// Read as: Less Than
// Usage: {{#if (lt x y)}} ... x < y ... {{/if}}
// Usage: {{#if (lte 2 x 8)}} ... 2 < x < 8 [x is between 2 and 8 not included]... {{/if}}
// Usage: {{#if (lte 2 x y 8)}} ... 2 < x < y < 8 [x and y are between 2 and 8 not included, and x < y]... {{/if}}
Handlebars.registerHelper('lt', function (...args) {
return args.slice(0, -1).map((x: any, i: number, v: any[]) => x < v[i+1]).slice(0, -1).every(x => x)
})
// Read as: Less Than or Equal
// Usage: {{#if (lt x y)}} ... x <= y ... {{/if}}
// Usage: {{#if (lte 2 x 8)}} ... 2 <= x <= 8 [x is between 2 and 8 included]... {{/if}}
// Usage: {{#if (lte 2 x y 8)}} ... 2 <= x <= y <= 8 [x and y are between 2 and 8 included, and x < y]... {{/if}}
Handlebars.registerHelper('lte', function (...args) {
return args.slice(0, -1).map((x: any, i: number, v: any[]) => x <= v[i+1]).slice(0, -1).every(x => x)
})
// Usage: {{#if (any x y)}} ... x || y ... {{/if}}
// Usage: {{#if (any x y z )}} ... x || y || z ... {{/if}}
// Usage: {{#if (any (eq x 1) (eq y 3) )}} ... x == 1 || y == 3 ... {{/if}}
Handlebars.registerHelper('any', function (...args) {
return args.slice(0, -1).some(x => x)
})
// Usage: {{#if (all x y)}} ... x && y ... {{/if}}
// Usage: {{#if (all x y z )}} ... x && y && z ... {{/if}}
// Usage: {{#if (all (eq x 1) (eq y 3) )}} ... x == 1 && y == 3 ... {{/if}}
Handlebars.registerHelper('all', function (...args) {
return args.slice(0, -1).every(x => x)
})
// Usage: {{#if (not x)}} ... !x ... {{/if}}
// Usage: {{#if (not (all x y z))}} ... !(x && y && z) ... (!x || !y || !z) {{/if}}
Handlebars.registerHelper('not', function (x) {
return !x
})
// Usage: {{sum x 1)}} => x + 1
// Usage: {{#if (eq x (sum y 1))}} ... x == y + 1 ... {{/if}}
// Usage: {{sum x y -8)}} => x + y - 8
// Usage: {{sum "Hello" " " "World" "!"}} => "Hello" + " " + "World" + "!" => "Hello World!"
Handlebars.registerHelper('sum', function (...args) {
return args.slice(0, -1).reduce((partialSum, a) => partialSum + a, 0);
})
// Usage: {{sub 0 x}} => -x
// Usage: {{sub x y}} => x - y
// Usage: {{sum w x (sub y z)}} => w + x + (y - z)
Handlebars.registerHelper('sub', function (...args) {
return args.slice(1, -1).reduce((partialSum, a) => partialSum - a, args[0]);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment