I am working on a template system. I imagine that as a regular user you can create a. json file, and based on that file the system will automatically generate html. I am fai
probably a bit late, i was gonna do something similar, and came across this thread, but have some code, the callback function is called from an XHR object which gets data from the currently static file "response.json"
function callback(req)
{
var response = eval("("+req.responseText+")");
response = response.response;
createElementsFromJSON(response, document.body);
}
function createElementsFromJSON(json, parent)
{
for(var i in json)
{
var object = json[i];
var obj = document.createElement(object.element);
for(var tag in object)
if (tag!="children"&&tag!="element")
obj.setAttribute(tag, object[tag]);
parent.appendChild(obj);
if (typeof(object.children)=="object")
createElementsFromJSON(object.children, obj);
}
}
JSON:
{
"response":
[
{
"element":"div",
"id":"james",
"children":
[
{
"element":"h1",
"id":"bob",
"innerHTML":"bobs content",
},
{
"element":"h2",
"id":"rob",
"innerHTML":"robs content",
},
{
"element":"p",
"innerHTML":"some random text",
},
],
},
{
"element":"div",
"id":"alex",
"innerHTML":"this is a test message in a div box",
},
]
}