Skip to content

Instantly share code, notes, and snippets.

@hail2u
Created August 4, 2013 11:56
Show Gist options
  • Save hail2u/6150133 to your computer and use it in GitHub Desktop.
Save hail2u/6150133 to your computer and use it in GitHub Desktop.
Handlebars.jsでSSIライクなインクルードを行うヘルパー関数 ref: http://qiita.com/hail2u/items/ea68e89355d25d357ee3
$ node include-helper.js test,tmpl
<p>apple is sweet and red.</p>
<p>orange is sweet and orange.</p>
<p>grape is sweet and purple.</p>
#!/usr/bin/env node
var fs = require('fs');
var handlebars = require('handlebars');
handlebars.registerHelper('include', function (f, d) {
return new handlebars.SafeString(applyTemplate(f, this));
});
function applyTemplate(f, d) {
var tmpl = handlebars.compile(fs.readFileSync(f, 'UTF-8'));
return tmpl(d);
}
console.log(applyTemplate(process.argv.slice(2).shift(), {
"fruits": [
{"name": "apple", "color": "red"},
{"name": "orange", "color": "orange"},
{"name": "grape", "color": "purple"}
]
}));
<p>{{name}} is sweet and {{color}}.</p>
{{#fruits}}{{include "include.tmpl"}}{{/fruits}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment