3

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.

mtt
  • 1,697
  • 1
  • 15
  • 20
  • Yes it's possible, it's easy, but for the building you'll need some code. What language do you want to use ? – Denys Séguret May 05 '13 at 16:55
  • Note that an alternative is to let the browser load the json file and handle everything in javascript, that's what I do in [one of my sites](http://dystroy.org/re7210/index.html). But if your site is big, that's not the most efficient solution. – Denys Séguret May 05 '13 at 16:57
  • @dystroy i'd like to use javascript (nodejs) but i can also use php if needed. pay attention i want to use static html file on my website, not running nodejs or php! :) – mtt May 05 '13 at 17:02
  • 1
    Maybe template engines like http://handlebarsjs.com/ can help you. – shakib May 05 '13 at 19:14

3 Answers3

2

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.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • that's okay but my html page is not that simple :) that was just an example :) including all the html page into the nodejs program is not that simple, isnt there anything on the web that does automatically what i need? – mtt May 05 '13 at 17:09
  • 1
    From there you'll have to do the rest of the work according to your precise requirements. I gave you the principle. – Denys Séguret May 05 '13 at 17:10
  • okay that's what i will do if i dont find something to do that automatically :) – mtt May 05 '13 at 17:11
  • What you "need" isn't clear. There is no equivalence between JSON and HTML, so there's no program making an obvious translation from one format to another one, this wouldn't make sense. – Denys Séguret May 05 '13 at 17:12
  • i know there is no equivalence between json and html. the "problem" with your solution is that i have to put the entire html page into fs.writeFile( ... ) and that's not fast nor easy. i wondered if there was something that could automatically put json object into an html page and render that page. i think first description is clear :( – mtt May 05 '13 at 17:41
0

Yeah, that is possible. I would recommend using php cause of same origin policy.. Just load the json from a file or directly and then create a new document with the required contents. It is easily done with phps file_put_contents().

Greets

-1

Great, one little thing. Looks like fs.writeFileSync() instead of fs.writeFile()

Boken
  • 4,825
  • 10
  • 32
  • 42
olsard7
  • 1
  • 1