Last active
December 31, 2016 18:04
-
-
Save tbranyen/2656508729664d7fb8709966ebeccf67 to your computer and use it in GitHub Desktop.
Possible way to improve tagged template usability for components
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
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