Skip to content

Instantly share code, notes, and snippets.

@o0101
Created May 23, 2017 06:27
Show Gist options
  • Save o0101/b329296f3912d9a550f8d56cba7777f9 to your computer and use it in GitHub Desktop.
Save o0101/b329296f3912d9a550f8d56cba7777f9 to your computer and use it in GitHub Desktop.
Easiest way to create HTML document fragments from text
"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