Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment