Skip to content

Instantly share code, notes, and snippets.

@tracend
Last active August 28, 2025 14:49
Show Gist options
  • Select an option

  • Save tracend/7522125 to your computer and use it in GitHub Desktop.

Select an option

Save tracend/7522125 to your computer and use it in GitHub Desktop.
Handlebars relational operators #handlebars #helper #cc

Handlebars relational operators

This is a series of MATLAB style relational operators for Handlebars.

Operators

  • eq - equal to
  • ne - not equal to
  • lt - less than
  • gt - greater than
  • le - less than or equal to
  • ge - greater than or equal to

With any helper add values a, b to be compared if their condition validates

Usage

Include the operators needed in your application. Then condition in your template, for example:

{{#eq a b}}
  content when condition validates
{{else}}
  content in any other case
{{/eq}}

Credits

Created by Makis Tracend

// equal
Handlebars.registerHelper('eq', function( a, b ){
var next = arguments[arguments.length-1];
return (a === b) ? next.fn(this) : next.inverse(this);
});
// greater than or equal to
Handlebars.registerHelper('ge', function( a, b ){
var next = arguments[arguments.length-1];
return (a >= b) ? next.fn(this) : next.inverse(this);
});
// greater than
Handlebars.registerHelper('gt', function( a, b ){
var next = arguments[arguments.length-1];
return (a > b) ? next.fn(this) : next.inverse(this);
});
// less than or equal to
Handlebars.registerHelper('le', function( a, b ){
var next = arguments[arguments.length-1];
return (a <= b) ? next.fn(this) : next.inverse(this);
});
// less than
Handlebars.registerHelper('lt', function( a, b ){
var next = arguments[arguments.length-1];
return (a < b) ? next.fn(this) : next.inverse(this);
});
// not equal
Handlebars.registerHelper('ne', function( a, b ){
var next = arguments[arguments.length-1];
return (a !== b) ? next.fn(this) : next.inverse(this);
});
@adamreisnz

Copy link
Copy Markdown

Simplified version for Node.js handlebars:

handlebars.registerHelper('eq', function(a, b) {
  return (a === b);
});
handlebars.registerHelper('gt', function(a, b) {
  return (a > b);
});
handlebars.registerHelper('gte', function(a, b) {
  return (a >= b);
});
handlebars.registerHelper('lt', function(a, b) {
  return (a < b);
});
handlebars.registerHelper('lte', function(a, b) {
  return (a <= b);
});
handlebars.registerHelper('ne', function(a, b) {
  return (a !== b);
});

Usage:

{{#if (gt someArray.length 1)}}
  Multiple items
{{else}}
  One item
{{/if}}

@hughevans

Copy link
Copy Markdown

This works fine in Node:

Handlebars.registerHelper("greaterThan", function (a, b) {
  var next = arguments[arguments.length - 1];
  return a > b ? next.fn() : next.inverse();
});

@MigueBlogs

Copy link
Copy Markdown

Good code !

@MarcosCastillo7

Copy link
Copy Markdown

Versión simplificada para manillares Node.js:

manillares . registerHelper ( 'eq' ,  function ( a ,  b )  { 
  return  ( a  ===  b ) ; 
} ) ; 
manillares . registerHelper ( 'gt' ,  function ( a ,  b )  { 
  return  ( a  >  b ) ; 
} ) ; 
manillares . registerHelper ( 'gte' ,  función( a ,  b )  { 
  return  ( a  > =  b ) ; 
} ) ; 
manillares . registerHelper ( 'lt' ,  function ( a ,  b )  { 
  return  ( a  <  b ) ; 
} ) ; 
manillares . registerHelper ( 'lte' ,  function ( a ,  b )  { 
  return  ( a <=  b ) ; 
} ) ; 
manillares . registerHelper ( 'ne' ,  function ( a ,  b )  { 
  return  ( a  ! ==  b ) ; 
} ) ;

Uso:

{{#if (gt someArray.length 1)}}
  Varios elementos
{{demás}}
  Un item
{{/Si}}

Disculpa donde coloco el código de Handlebars, donde pego las funciones o como las configuro, soy nuevo, alguien me podría ayudar se los agradeceria

@IArnaut2

Copy link
Copy Markdown

How come Handlebars does not support these operators already???

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment