Skip to content

Instantly share code, notes, and snippets.

@terion-name
Last active December 30, 2015 18:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save terion-name/d87ed8907f1bb2e25f32 to your computer and use it in GitHub Desktop.
Save terion-name/d87ed8907f1bb2e25f32 to your computer and use it in GitHub Desktop.
Ember Handlebars "ternary" helper

Ternary helper for Ember Handlebars

With two parameters: if first parameter evaluates to true it will be printed, otherwise second

{{iftrue project.price 'N/A'}} // $9.99
{{iftrue project.priceNotAvailable 'N/A'}} // N/A

With three parameters: if first parameter evaluates to true second will be printed, otherwise third

// If deadline is set formatted date will be printed, otherwise 'N/A'
{{iftrue project.deadline (moment-format project.deadline 'DD.MM.YYYY') 'N/A'}} 
// app/helpers/iftrue.js
import Ember from 'ember';
export function iftrue(params) {
if (params[0]) {
return params.length === 2 ? params[0] : params[1];
}
if (params.length === 2) {
return params[1];
} else if (params.length === 3) {
return params[2];
}
return null;
}
export default Ember.Helper.helper(iftrue);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment