Home handlebars.choice

Handlebars Choice helper

{{#choose x}}
    {{#choice "a"}} A {{/choice}}
    {{#choice "b"}} B {{/choice}}
{{/choose}}

Content selection helpers providing pluralisation/inflection for Handlebars

Version

1.0.1

Installation

npm install handlebars.choice

Registering the helpers

var Handlebars = require("handlebars");
var Choice = require("handlebars.choice");
Choice.registerHelpers(Handlebars);

Using the helpers

Strings are treated as straight keywords

{{#choose x}}
    {{#choice "a"}} A {{/choice}}
    {{#choice "b"}} B {{/choice}}
{{/choose}

Context
{x: "a"}           → « A »
{x: "b"}           → « B »
{x: "c"}           → « »

All matched choices are output - choices can have multiple keywords

{{#choose x}}
    {{#choice "a c"}} AC {{/choice}}
    {{#choice "b c"}} BC {{/choice}}
    {{#choice "a d"}} AD {{/choice}}
{{/choose}

Context
{x: "a"}           → « AC AD »
{x: "b"}           → « BC »
{x: "c"}           → « AC BC »
{x: "d"}           → « AD »

Numbers are processed as inflection keywords

{{#choose x}}
    {{#choice "zero other"}} NON-SINGULAR {{/choice}}
    {{#choice "one"}} SINGULAR {{/choice}}
{{/choose}

Context
{x: 0}             → « NON-SINGULAR »
{x: 1}             → « SINGULAR »
{x: 2}             → « NON-SINGULAR »

Booleans are processed as boolean keywords

{{#choose x}}
    {{#choice "true"}} TRUE {{/choice}}
    {{#choice "false"}} FALSE {{/choice}}
{{/choose}}

Context
{x: true}          → « TRUE »
{x: false}         → « FALSE »

Alternative choosing functions can be passed

var fn = function (input) {
    return input >= 10;
};

{{#choose x function=fn}}
    {{#choice "high"}} HIGH {{/choice}}
    {{#choice "low"}} LOW {{/choice}}
{{/choose}}

Context
{x: 10}            → « HIGH »
{x: 9}             → « LOW »

See choose helper for more details

Additional methods

Choice.locale

Choice.locale([loc]);

Get (or set) Choice’s current locale

See handlebars.choice.locale

Choice.registerHelpers

Choice.registerHelpers(handlebars);

Register Choice helpers with Handlebars

See handlebars.choice.registerHelpers

Tests

npm test

To see output generated by tests

npm run test:output