Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@niksumeiko
Created October 1, 2013 12:02
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save niksumeiko/6777429 to your computer and use it in GitHub Desktop.
Save niksumeiko/6777429 to your computer and use it in GitHub Desktop.
Handlebars.js templates engine custom IF condition helper. Allows to compare values one to each other like you are used to in programming.
// Compares first value to the second one allowing entering IF clouse if true.
// Otherwise entering ELSE clause if exist.
Handlebars.registerHelper('ifEquals', function(a, b, options) {
if (a === b) {
return options.fn(this);
}
return options.inverse(this);
});
{{#ifEquals mediaType 'video'}}
{{!-- If our mediaType is equals to 'video', displaying video player. --}}
<video></video>
{{/ifEquals}}
{{#ifEquals userFullname 'Nik Sumeiko'}}
<p>Custom HTML for me.</p>
{{else}}
<p>Global HTML for anyone else.</p>
{{/ifEquals}}
@davidjray
Copy link

This ifEquals helper was perfect for me. Thank you.

@tomenden
Copy link

Thank you!

@aseredenko
Copy link

You save my time! Many thanks!

@kenold
Copy link

kenold commented May 16, 2018

Simple yet powerful. Thank you.

@victorelexpe
Copy link

Thanks!! You saved my time

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