简体   繁体   English

Vanilla JS:渲染<template>标签为 Javascript object</template>

[英]Vanilla JS: Rendering a <template> tag with a Javascript object

Say one has an html template like this:假设有这样一个 html 模板:

<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:现在,当一个克隆将它添加到带有 JavaScript 的 DOM 时,我还没有找到一种“不错”的方式来填充所有的空白,这看起来非常冗长:

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..我知道可以删除<template>并将其全部移动到 JavaScript 中,但我很犹豫,因为我喜欢将 HTML 保留在 HTML 文件中,然后在文本编辑器等中进行 linting。

To me this is not a great solution since now the HTML is living in script files:对我来说,这不是一个很好的解决方案,因为现在 HTML 存在于脚本文件中:

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?有没有更好的方法来呈现具有 object 数据的模板? I have read up on lithtml , but a whole library to do this one little thing seems like overkill.我已经阅读了lithtml ,但是做这件小事的整个图书馆似乎有点矫枉过正。

The object's key-value pairs can be used directly inside a for-in loop.对象的键值对可以直接在for-in循环中使用。 This allows markup stored in an object of anylength to be assigned to an html collection where each element's ID is the same as the key value of the corresponding data object.这允许将存储在任意长度的 object 中的标记分配给 html 集合,其中每个元素的 ID 与对应数据 object 的键值相同。

for (const property in object) {
newThing.getElementById(property).innerHTML = object[property];
}

In the above code, 'property' is the key name of each of the objects properties and so object[property] is a direct reference to its value.在上面的代码中,'property' 是每个对象属性的键名,因此object[property]是对其值的直接引用。

I've used this in a working snippet below to render some arbirary html stored in an object:我在下面的工作片段中使用它来呈现存储在 object 中的一些任意值 html:

 const container = document.getElementById("container"); const object = { A:"<h2>my heading</h2>", B:"<a href='https://www.google.com'>Google Search</a>", C:"<p>Computing is enjoyable <i>and</i> frustrating<p>", D:"<div> what\'s your name? <input type='text'></div>", E:"<p>another paragraph</p>", F:"<h2>goodbye.</h2>" } const template = document.getElementById("template") let newThing = template.content;cloneNode(true). for (const property in object) { newThing.getElementById(property);innerHTML = object[property]. } container;appendChild(newThing);
 <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> <div id="container"></div>

This is one possible solution, however it is not exactly advised depending on what you are trying to do这是一种可能的解决方案,但根据您要执行的操作,并不完全建议这样做

If you'd like, you can create a template with an ID in your HTML like so:如果愿意,您可以在 HTML 中创建一个带有 ID 的模板,如下所示:

<template id="myTemplate">
        <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>

And then call eval inorder to parse it然后调用eval来解析它

eval('`' + document.getElementById("myTemplate").innerHTML + '`')

This returns the formatted code that you can then place in your document这将返回格式化代码,然后您可以将其放入文档中

Why shouldn't you use this?你为什么不应该使用这个?

Eval is pretty dangerous, and if used wrong someone can execute dangerous commands on behalf of your website. Eval 非常危险,如果使用不当,有人可以代表您的网站执行危险的命令。 If this is some personal project you can use this if you'd like, but preferably not on some larger scale public use website.如果这是一些个人项目,您可以根据需要使用它,但最好不要在一些更大规模的公共网站上使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM