Created
May 23, 2017 06:27
-
-
Save o0101/b329296f3912d9a550f8d56cba7777f9 to your computer and use it in GitHub Desktop.
Easiest way to create HTML document fragments from text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
{ | |
function df( t ) { | |
return (new DOMParser).parseFromString(`<template>${t}</template>`,"text/html").head.firstChild.content; | |
} | |
function fc( t ) { | |
const article = document.createElement('article'); | |
article.innerHTML = t; | |
return article.firstChild; | |
} | |
// then you can do cool things like | |
function component( { a, b, c } ) { | |
return df( ` | |
<article> | |
<h1>${ a }</h1> | |
<p>${ b }</p> | |
<img src=${ c }> | |
</article> | |
`); | |
} | |
// export | |
Object.assign( self, { | |
df, | |
fc | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment