Skip to content

Instantly share code, notes, and snippets.

@arnabdas
Created April 7, 2015 16:37
Show Gist options
  • Select an option

  • Save arnabdas/f5240cb074ac85ee4ceb to your computer and use it in GitHub Desktop.

Select an option

Save arnabdas/f5240cb074ac85ee4ceb to your computer and use it in GitHub Desktop.
Nested template generation by Handlebars
[
{
"name": "abcd",
"children": [
{
"name": "fgtf",
"children": [
]
},
{
"name": "rtgh",
"children": [
{
"name": "aser",
"children": [
]
},
{
"name": "aser",
"children": [
]
}
]
}
]
}
];
(function () {
var jf = require('jsonfile'),
fs = require('fs'),
handlebars = require('handlebars'),
util = require('util');
var source =
'<ul>' +
' {{#children}}' +
' {{> list}}' +
' {{/children}}' +
'</ul>';
handlebars.registerPartial('list',
' <li>' +
' {{name}}' +
' {{#children}}' +
' <ul>' +
' {{> list}}' +
' </ul>' +
' {{/children}}' +
' </li>');
var template = handlebars.compile(source);
jf.readFile('data.json', function(err, obj) {
var generatedString = template({children: obj});
//console.log(util.inspect(obj));
fs.writeFile('./build.html', generatedString, function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment