If condition

More of a general helper with handy if conditions. Its a must have helper.

handlebars = handlebars.create({
    helpers: {
	ifCond: (v1, operator, v2, options) => { 
            switch(operator){
                case'==':
                    return(v1 === v2) ? options.fn(this) : options.inverse(this);
                case'!=':
                    return(v1 !== v2) ? options.fn(this) : options.inverse(this);
                case'===':
                    return(v1 === v2) ? options.fn(this) : options.inverse(this);
                case'<':
                    return(v1 < v2) ? options.fn(this) : options.inverse(this);
                case'<=':
                    return(v1 <= v2) ? options.fn(this) : options.inverse(this);
                case'>':
                    return(v1 > v2) ? options.fn(this) : options.inverse(this);
                case'>=':
                    return(v1 >= v2) ? options.fn(this) : options.inverse(this);
                case'&&':
                    return(v1 && v2) ? options.fn(this) : options.inverse(this);
                case'||':
                    return(v1 || v2) ? options.fn(this) : options.inverse(this);
                default:
                    return options.inverse(this);
            }
        }
    }
});
The helper
{{#ifCond myVariable '!=' 'somevalue'}}
	<h1>Display things</h1>
{{/ifCond
Usage
{{#ifCond myVariable '!=' 'somevalue'}}
	<h1>Display things</h1>
{{else}}
	<h1>Display different things</h1>
{{/ifCond
Using with an else

Checkbox checked state

When you have a bool value stored in your DB you may want to set the state of a checkbox with that value.

handlebars = handlebars.create({
    helpers: {
	checkedState: (state) => {
            if(state === 'true' || state === true){
                return 'checked';
            }
            return'';
        }
    }
});
Checked state of a checkbox
<div class="form-group">
    <label for="checkbox" class="control-label">Checkbox</label>
    <div class="checkbox">
    	<label>
            <input type="checkbox" {{checkedState myvar}} id="checkbox">
        </label>
    </div>
</div>
Usage

Select/dropdown selected state

Similar to the checkState helper you may want to set the selected item in a <select> with the value from your DB etc.

handlebars = handlebars.create({
    helpers: {
	selectState: (state, value) => {
            if(state === value){
                return 'selected';
            }
            return'';
        }
    }
});
Selected state of a select list
<div class="form-group">
    <label class="control-label">Country *</label>
    <select class="form-control" id="country" name="country">
        <option value="" disabled selected>Select Country</option>
        {{#each countryList}}
            <option {{selectState this db.country}} value="{{this}}">{{this}}</option>
        {{/each}}
    </select>
</div>
Usage

Math

A general math function for simple math.

handlebars = handlebars.create({
    helpers: {
	math: (lvalue, operator, rvalue, options) => {
            lvalue = parseFloat(lvalue);
            rvalue = parseFloat(rvalue);

            return{
                '+': lvalue + rvalue,
                '-': lvalue - rvalue,
                '*': lvalue * rvalue,
                '/': lvalue / rvalue,
                '%': lvalue % rvalue
            }[operator];
   	}
    }
});
General Math function
{{math orderTotal '-' orderDiscount}}
Usage

Stringify an object to see its contents

handlebars = handlebars.create({
    helpers: {
        stringify: (obj) => {
            if(obj){
                return JSON.stringify(obj);
            }
            return'';
        }
    }
});
Stringify and object
{{stringify myObject}}
Usage to display a stringified object