Skip to content

Instantly share code, notes, and snippets.

@kgust
Last active December 20, 2017 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kgust/f8fb200945a7cc3d2429e9a3a3de21e6 to your computer and use it in GitHub Desktop.
Save kgust/f8fb200945a7cc3d2429e9a3a3de21e6 to your computer and use it in GitHub Desktop.
We should be using the <template> tag instead of <script type="text/x-template">.
<!doctype html>
<!-- Most of this information comes from:
https://www.html5rocks.com/en/tutorials/webcomponents/template/
Apple's propsal adds variables:
https://github.com/w3c/webcomponents/blob/gh-pages/proposals/Template-Instantiation.md
-->
<html>
<head>
<style>
.bordered { border: 1px solid #ddd; border-radius: 4px; }
div { margin-bottom: 5px; }
</style>
</head>
<body>
<template id="mytemplate">
<div class="bordered">
<p>This is from the &lt;template&gt; tag.</p>
<img src="" alt="great image">
<div class="comment">This is a comment!</div>
</div>
</template>
<blahblah id="nonsense_tag">
<div class="bordered">
<p>
Nonsense tags act like a div, but not in all aspects.
Style could be applied to this inner div but not &lt;blahblah&gt; itself.</p>
<img src="" alt="great image">
<div class="comment">This is another comment!</div>
</div>
</blahblah>
<script>
function supportsTemplate() {
return 'content' in document.createElement('template');
}
if (supportsTemplate()) {
var t = document.querySelector('#mytemplate');
// Populate the src at runtime.
t.content.querySelector('img').src = 'logo.png';
var clone = document.importNode(t.content, true);
document.body.appendChild(clone);
} else {
// using display:none hides this from IE
document.getElementById('mytemplate').style.display = 'none';
/* Use older template techniques here. */
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment