0.0.2 • Published 4 years ago
flatbars v0.0.2
Flatbars
A simple CLI tool for templating using Handlebars
.
For information on syntax and other Handlebars specifics check out this guide.
It might be installed as a global tool to render a simple mustache/handlebars template of some kind
$ npm install -g flatbars
$ flatbars dataView.json myTemplate.handlebars > output.html
or as a package.json devDependency in a build process
$ npm install flatbars --save-dev
{
"scripts": {
"build": "flatbars dataView.json myTemplate.handlebars > dist/output.html"
}
}
$ npm run build
Helpers
In order to use Handlebar helpers you just need to create a js
file that exposes a register
function. This function is going to be called with the Handlebars instance as first argument.
// helpers.js
const helpers = () => { }
helpers.register = function (Handlebars) {
Handlebars.registerHelper('loud', (s) => s.toUpperCase());
}
module.exports = helpers;
<!-- myTemplate.handlebars -->
{{loud title}}
{
"title": "This is my title!"
}
To render just run this:
$ flatbars dataView.json myTemplate.handlebars helpers.js > output.html
Partials
Using partials on your template is also simple, you just pass paths to partials using -p
flag:
<!-- myTemplate.handlebars -->
<ul class="people_list">
{{#each people}}
<li>{{> mypartial}}</li>
{{/each}}
</ul>
<!-- myPartial.handlebars -->
{{firstname}} {{loud lastname}}
{
"people": [
{
"firstname": "John",
"lastname": "Doe"
},
{
"firstname": "Janne",
"lastname": "Smith"
},
{
"firstname": "Richard",
"lastname": "Miles"
}
]
}
$ flatbars -p partial.handlebars dataView.json myTemplate.handlebars > output.html
stdin support
It also supports stdin, just pass '-'
as your data view argument:
$ cat dataView.json | flatbars -p partial.handlebars - myTemplate.handlebars helpers.js > output.html