Helpers for Handlebars templates used at FishBrain
npm install fishbars
var handlebars = require("handlebars");
var fishbars = require("fishbars");
fishbars.registerHelpers(handlebars, {
language: 'sv'
});
var template = handlebars.compile("{{translate greeting}} {{name}}");
var result = template({ name: 'jakob', greeting: { sv: 'hej', en: 'hi' } });
console.log(result); // hej jakob
There is one function independent of user settings:
image
It take three arguments; a comparison operator, a size and an array of photos. It then returns the photo that satisfy the given given comparions. If more than one does, it selects the one that is closest to the requirement.
var template = handlebars.compile("{{image '>=' '64x64' photos }}");
var result = template({
photos: photos: [{
url: "file1.png",
size: "140x140"
}, {
url: "file2.png",
size: "600x600"
}]
});
console.log(result); // file1.png
There are also five functions depending on user settings:
translate
weight
length
temperature
speed
Translate is used as in the "getting started"-example above. Given a string as language
in registerHelpers
, that key is looked up when translate
is called on an object in the template. You can use any language code you want; to the function it's just a key in a lookup.
The remaining four functions are used to convert a number (given in SI-units) to a string representation in the configured unit, including the unit abbreviation. For example, getting my length in feet would go like this:
var handlebars = require("handlebars");
var fishbars = require("fishbars");
fishbars.registerHelpers(handlebars, {
units: {
weight: 'lb'
}
});
var template = handlebars.compile("{{name}} is {{length myLength}} tall");
var result = template({ name: 'jakob', myLength: 1.81 }); // note that length is given in meters
console.log(result); // Jakob is 5.94 ft tall
The full configuration would look like this:
fishbars.registerHelpers(handlebars, {
language: 'sv',
units: {
weight: 'kg', // or 'lb'
length: 'm', // or 'ft'
temperature: 'C', // or 'F'
speed: 'm/s' // or 'km/h' or 'mph' or 'kn'
}
});
Make sure you have node installed. Running node -v
should give you v0.10.29 or above.
Run npm install
to install the dependencies of the project.
Run npm test
to run all the tests. It will also show you the test coverage on completion.
You can run a subset of the tests using TESTS="some pattern" npm test
, where the pattern is matched to the names of the tests.
In order to release a new version of the library, run make release VERSION=x.x.x
. Your package file, git tags, coveralls and the global npm repository will all automatically be updated (but only if the tests pass, your working tree is clean and if you're on the right branch - master of course).