As the title says, I am trying to write a function which would create some HTML based on the array I feed it. If the array were simply one dimensional, I wouldn't have any problems. But the "multidimensionality" of the array is where I'm having issues. Namely since it's not always consistent.
I should also note that I am trying to accomplish this without any 3rd party JS libraries.
For example, here is a sample array:
var template = [
['div', {id: 'wrapper'}, [
['link', {rel:'stylesheet', href:'//mysite.com/css.css', type:'text/css'}],
['header', "Look at me! I'm a header!"],
['nav', {class:'main-nav'}, [
['ul', [
['li', ['a', {'href':'/home'}, "Home"]],
['li', ['a', {'href':'/about'}, "About Us"]],
['li', ['a', {'href':'/erase_internet.php'}, "Don't click me!"]]
]]
]],
['section', "Some sample text!"],
['footer', "Copyright © 1984"]
]]
];
The format of the array is:
[string "type" [, json obj "attributes" ][, string "text"][, array "children"]]
Now I already have one function which takes a single array object and creates an element:
function createEl(type, attr, text) {
var key, el = d.createElement(type);
if (typeof attr === 'object' && !Array.isArray(attr)) {
for (key in attr) {
if (attr.hasOwnProperty(key)) {
el.setAttribute(key, attr[key]);
}
}
}
else if (typeof attr === 'string' && text.length > 0) {
el.appendChild(d.createTextNode(attr));
}
if (typeof text === 'string' && text.length > 0) {
el.appendChild(d.createTextNode(text));
}
return el;
}
But I want to be able to process all of the "children" and append them to their parents, as indicated by the sample array, so that the output would look like this:
<div id="wrapper">
<link rel="stylesheet" href="//mysite.com/css.css" type="text/css" />
<header>Look at me! I'm a header!</header>
<nav class="main-nav">
<ul>
<li><a href="/home">Home</a></li>
<li><a href="/about">About us</a></li>
<li><a href="/erase_internet.php">Don't click me!</a></li>
</ul>
</nav>
<section>Some sample text!</section>
<footer>Copyright © 1984</footer>
</div>
So here are my questions:
- If I don't know how many levels deep the array goes, what's the best-practice method for traversing through the
children
and all of the grand-children
? - Would I call the
createEl()
function again from within itself to create and append those children elements, if they exist?- Is that even possible?
- Would it help at all if I changed the structure of the array to this?:
[string "type" [, json obj "attributes" [, string "text" [, array "children"]]]]
- Is there maybe a better way of doing this altogether, without having to resort to jQuery or the like? (subjective, but I value the expertise and experience of the SO Community)
Many thanks in advance!
class
on line 5 oftemplate
in quotes because it is actually a reserved word in JS (sorry) – user2521439 Mar 1 at 21:12class
in my code, but I did fail to encapsulate that in my example. Thanks for pointing that out! – DondeEstaMiCulo Mar 1 at 21:16class
keyword is being implemented in ES6 – user2521439 Mar 1 at 21:17