Template Tag

ÅÛÇø´ÀÇ Æ¯Â¡Àº ´ÙÀ½°ú °°´Ù.

  • html  Äڵ带 ¹Ýº¹ÀûÀ¸·Î »ç¿ëÇÒ¶§ template ű׸¦ »ç¿ëÇϸé Æí¸®ÇÏ´Ù.
  • innerHTMLÀ» »ç¿ëÇϸé HTML ÄÚµåÀÇ °¡µ¶¼ºÀÌ ¶³¾îÁö±â ¶§¹®¿¡ ÅÛÇø´À» »ç¿ëÇϸé ÁÁ´Ù.
  • ÅÛÇø´Àº ·»´õ¸µÀ» ÇÏÁö ¾Ê´Â´Ù.

template tag ¿¹Á¦ 1

½ÇÇà È­¸éÀÌ´Ù.



ÅÛÇø´ÀÇ ³»¿ëÀ» °¡Á® ¿Ã¶§´Â ".content"·Î °¡Á®¿Í¾ß ÇÑ´Ù.
".content"¸¦ »©¸é ¾Æ¹«°Íµµ ·»´õ¸µ ÇÏÁö ¾ÊÀ» °ÍÀÌ´Ù.
.content ¼Ó¼ºÀº ÅÛÇø´ÀÇ ³»ºÎ¸¦ Æ÷ÇÔÇÏ´Â Àбâ Àü¿ëÀÇ DocumentFragmentÀÌ´Ù.

//Template Tag
<template>
<style>
    .center-text
    {
        display: flex;
        flex-direction: row;
        justify-content: center;
        align-items: center;
    }

    .list-item
    {
        background-color: gold;
        margin: 1em;
        width: 20em;
        height: 3em;
    }
</style>
<div class="center-text list-item">template test</div>
</template>

//javascript code
<script>
function showTemplate() {
    var temp = document.getElementsByTagName("template")[0];
    var clone = temp.content.cloneNode(true);
    document.body.appendChild(clone);
}

showTemplate();
showTemplate();
showTemplate();
</script>

template¸¦ ã´Â´Ù
cloneNode·Î º¹Á¦ÇÑ´Ù.
appendChild·Î Ãß°¡ÇÑ´Ù.

Å×½ºÆ® : template_test.html


template tag ¿¹Á¦ 2

//Template Tag
<template id="mytemplate">
<style>
    .center-text
    {
        display: flex;
        flex-direction: row;
        justify-content: center;
        align-items: center;
    }

    .list-item
    {
        background-color: gold;
        margin: 1em;
        width: 20em;
        height: 3em;
    }
</style>
<div class="center-text list-item">template test</div>
</template>

//javascript code
<script>
function showTemplate() {
    //var template = document.querySelector('#mytemplate');
    var template = document.getElementById('mytemplate');
    var clone = document.importNode(template.content, true);
    var host = document.querySelector('#host');
    host.appendChild(clone);
}

showTemplate();
showTemplate();
showTemplate();
</script>

id·Î template¸¦ ã´Â´Ù
importNode º¹Á¦ÇÑ´Ù.
appendChild·Î Ãß°¡ÇÑ´Ù.

Å×½ºÆ®: template_id.html

template tag ¿¹Á¦ 3

//Template Tag
<template id="mytemplate">
<style>
    .center-text
    {
        display: flex;
        flex-direction: row;
        justify-content: center;
        align-items: center;
    }

    .list-item
    {
        background-color: gold;
        margin: 1em;
        width: 20em;
        height: 3em;
    }
</style>
<div class="center-text list-item">template test</div>
</template>

<div id="host"></div>

//javascript code
<script>
function showTemplate() {
    var template = document.querySelector('#mytemplate');
       
    var fragment = document.createDocumentFragment();
    for(var n = 0; n < 10; ++n) {
        var clone = document.importNode(template.content, true);
        fragment.appendChild(clone);
    }
    
    var host = document.querySelector('#host');
    host.appendChild(fragment);
}

showTemplate();
</script>

DocumentFragment¸¦ ÀÌ¿ëÇÏ¿© ÅÛÇø´À» 10°³ ±×¸®°í ÀÖ´Ù.

Å×½ºÆ®: template_fragment.html

Âü°í)
https://www.html5rocks.com/ko/tutorials/webcomponents/template/
https://www.webcomponents.org/community/articles/introduction-to-template-element
https://qiita.com/amamamaou/items/55225b3873b098b9b2bd

À¥ ÄÄÆ÷³ÍÆ®¿¡ ´ëÇؼ­
http://ryuxia.tistory.com/m/2
http://html5rocksko.blogspot.com/2014/02/mashup-web-component-evolution-of-web-development.html
https://kyu.io/ko/À¥-ÄÄÆ÷³ÍÆ®3%e2%80%8a-%e2%80%8a½¦µµ¿ì-µ¼shadow-dom/