htmljsontemplatestemplate-enginestatic-html

html template + json data -> static html deploy


is it possible to have:
- a static html template
- a JSON with some data
and create static html file(s)?

For example i have to make a portfolio and i code html template:

...
<h1> {title} </h1>
<p> {description} </p>
...

Then i have a JSON like this:

"first work" : {
    "title" : "alpha",
    "description" : "lorem ipsum"
},
"second work" : {
    "title" : "beta",
    "description" : "lorem ipsum"
}

I want to "deploy" my website and have 2 static html file
first_work.html

<h1> alpha </h1>
<p> lorem ipsum </p>

second_work.html

<h1> beta </h1>
<p> lorem ipsum </p>

I know Jekyll that uses markdown to produce static html but i prefer JSON in this situation.


Solution

  • I just wrote a complete program in node.js doing it :

    var fs  = require("fs");
    var f = fs.readFileSync('./site.json').toString();
    var pages = JSON.parse(f);
    for (var key in pages) {
        var page = pages[key];
        fs.writeFile(
            key.replace(/ /g, '_')+'.html',
            '<h1>'+page.title+'</h1>'
            + '<p>'+page.description+'</p>'
        );
    }
    

    If your JSON is in a file named site.json (note that a comma is missing in your JSON), it writes the two HTML files.