Created
August 4, 2013 11:56
-
-
Save hail2u/6150133 to your computer and use it in GitHub Desktop.
Handlebars.jsでSSIライクなインクルードを行うヘルパー関数 ref: http://qiita.com/hail2u/items/ea68e89355d25d357ee3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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"} | |
] | |
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<p>{{name}} is sweet and {{color}}.</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{#fruits}}{{include "include.tmpl"}}{{/fruits}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment