Skip to content

Instantly share code, notes, and snippets.

@tracend
Last active April 12, 2023 16:36
Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tracend/7522125 to your computer and use it in GitHub Desktop.
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

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

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

Good code !

@MarcosCastillo7
Copy link

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

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