Say one has an html template like this:
<template id="template">
<div id="A"></div>
<div id="B"></div>
<div id="C"></div>
<div id="D"></div>
<div id="E"></div>
<div id="F"></div>
</template>
Now when one clones to add it to the DOM with JavaScript, I have not come across a "nice" way to fill in all the blanks, this seems very verbose:
const object = {A:1, B:2, C:3, D:4, E:5, F:6};
const template = document.getElementById("template")
let newThing = template.cloneNode(true);
newThing.getElementById("A").innerHTML = object.A;
newThing.getElementById("B").innerHTML = object.B;
newThing.getElementById("C").innerHTML = object.C;
newThing.getElementById("D").innerHTML = object.D;
newThing.getElementById("E").innerHTML = object.E;
newThing.getElementById("F").innerHTML = object.F;
container.appendChild(newThing);
I understand one could drop the <template> and move it all into JavaScript, but I am hesitant, because I like to keep HTML in HTML files, then you have linting in the text editor etc..
To me this is not a great solution since now the HTML is living in script files:
const newThing = `
<div>
<div>${object.A}</div>
<div>${object.B}</div>
<div>${object.C}</div>
<div>${object.D}</div>
<div>${object.E}</div>
<div>${object.F}</div>
</div>
Is there a better way to render a template with an object of data? I have read up on lithtml, but a whole library to do this one little thing seems like overkill.
for-inloop containing the single line:newThing.getElementById(property).innerHTML = object[property];ida key to a map which maps to the value, then loop through the entries.for inloop passes each of an object's property names to the loop, so the(property)in my exampe will be read as(A)on the first loop, with theinnerHTMLbeing set to the value of property A byobject[property], and so on for B, C, etc on subsequent iterations. It's a ready-to-go means of sequentially extracting key/value pairs from an object. Sorry if I've misunderstood. I'll post a snippet.