Skip to content

Instantly share code, notes, and snippets.

@mccallofthewild
Created January 27, 2020 20:51
Show Gist options
  • Select an option

  • Save mccallofthewild/2d1ae286618496d74d8259f80c851c21 to your computer and use it in GitHub Desktop.

Select an option

Save mccallofthewild/2d1ae286618496d74d8259f80c851c21 to your computer and use it in GitHub Desktop.
function decodeHtml(html) {
var txt = document.createElement('textarea');
txt.innerHTML = html;
return txt.value;
}
let renderTemplateElements = (state: State) => {
const stateful = document.querySelectorAll('[stateful]');
const elementsToTemplates = new Map<Element, string>();
function renderer(state: State) {
stateful.forEach(el => {
const template = elementsToTemplates.get(el) || decodeHtml(el.innerHTML);
elementsToTemplates.set(el, template);
try {
console.log(template);
el.innerHTML =
new Function(
`console.log(arguments); var state = arguments[0]; return ${template}`
)(state) || '';
} catch (e) {
el.innerHTML = '';
console.error(e);
}
});
}
renderTemplateElements = renderer;
return renderer(state);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment