Created
April 7, 2015 16:37
-
-
Save arnabdas/f5240cb074ac85ee4ceb to your computer and use it in GitHub Desktop.
Nested template generation by Handlebars
This file contains hidden or 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
| [ | |
| { | |
| "name": "abcd", | |
| "children": [ | |
| { | |
| "name": "fgtf", | |
| "children": [ | |
| ] | |
| }, | |
| { | |
| "name": "rtgh", | |
| "children": [ | |
| { | |
| "name": "aser", | |
| "children": [ | |
| ] | |
| }, | |
| { | |
| "name": "aser", | |
| "children": [ | |
| ] | |
| } | |
| ] | |
| } | |
| ] | |
| } | |
| ]; |
This file contains hidden or 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
| (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