I'm trying to build a quick and dirty static site generator for myself.
Let's say I have this test.html file:
{title}
{downloadpath}
This is my current.json where I get the values i want to replace:
{
"id": 123,
"album" : [{
"title": "Test EP",
"albumid": 1234,
"path": "test.zip"
}]
}
My replacement function looks like this:
// Iterate through JSON object and replace
function iterate(obj) {
for (var property in obj) {
if (obj.hasOwnProperty(property)) {
if (typeof obj[property] == "object")
iterate(obj[property]);
else
console.log("replace {" + property + "} with " + obj[property] )
htmldata.replace(/\{property\}/g, obj[property]);
}
}
}
iterate(json)
var result = htmldata
console.log(result)
// Write new HTML
fs.writeFile("test-" + json.id + ".html", result, 'utf8', function (err) {
if (err) {
return console.log(err);
}
});
and if I run it it works like this:
replace {id} with 123
replace {title} with Test EP
replace {albumid} with 1234
replace {path} with test.zip
{title}
{path}
and you can see the problem right there. I think it's always replacing the edited file with the input file so I don't see any changes. I can't figure it out and if someone could point me in the right direction I'd be grateful.
Thanks!
_.templatefunction and skip the self-rolling-template-function