Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Templates

Addy Osmani edited this page Aug 19, 2013 · 2 revisions

Templates

The HTML Templates specification is the normative description of this part of Web Components.

The <template> element contains markup intended to be used later. The content of the

The <template> element has property called content which holds the content of the template in a document fragment. When the author wants to use the the content they can move or copy the nodes from this property:

<template id="commentTemplate">
    <div>
        <img src="">
        <div class="comment-text"></div>
    </div>
</template>
<script>
function addComment(imageUrl, text) {
  var t = document.querySelector("#commentTemplate");
  var comment = t.content.cloneNode(true);
  // Populate content.
  comment.querySelector('img').src = imageUrl;
  comment.querySelector('.comment-text').textContent = text;
  document.body.appendChild(comment);
}
</script>
Clone this wiki locally