Skip to content

Instantly share code, notes, and snippets.

@tbranyen
Last active December 31, 2016 18:04
Show Gist options
  • Save tbranyen/2656508729664d7fb8709966ebeccf67 to your computer and use it in GitHub Desktop.
Save tbranyen/2656508729664d7fb8709966ebeccf67 to your computer and use it in GitHub Desktop.
Possible way to improve tagged template usability for components
import { html, innerHTML } from 'diffhtml';
// Import arbitrary template engine...
import combyne from 'combyne';
// Create the combyne template instance and set context to be the component's props.
const combyneProcessor = props => text => combyne(text).render(props);
// Experimental stateful components.
class MyComponent {
render(props) {
const processAsTemplate = combyneProcessor(props);
// Experimental feature that allows you to pre-process the flattened text
// template contents before tagged template interpolation.
return html.pre(processAsTemplate)`
<h1>{{message}}</h1>
{%if friends.length%}
<ul>
{%each friends as friend%}
<li>{{friend|toTitleCase}}</li>
{%endeach%}
</ul>
{%endif%}
`;
}
}
const friends = ['Fred', 'Ted', 'Gary'];
innerHTML(document.body, html`<${MyComponent} message="Hello world" friends=${friends} />`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment