JSFiddle - React, Tailwind, and code Playground

by damngoto

HTML

<script src="http://cdn.ractivejs.org/edge/ractive.js"></script>
<main></main>

<script id='template' type='text/ractive'>
    <button on-click='updateTemplate'>update template</button>

    <h1>Hello, {{name}}!</h1>
    <input value='{{name}}'/>
</script>

<script id='newTemplate' type='text/ractive'>
    <h1>Well, {{name}}, we updated the template.</h1>
    <input value='{{name}}'/>
</script>

CSS

body {
    font-family: 'Helvetica Neue', arial, sans-serif;
    font-weight: 200;
    color: #353535;
}

h1, h2, h3, h4, h5, h6 {
    font-weight: 200;
}

JavaScript

Ractive = Ractive.extend({
    beforeInit: function ( options ) {
        options.partials = options.partials || {};
        options.partials.template = extractTemplate( options.template );
        
        options.template = '{{#rendered}}{{>template}}{{/rendered}}';
    },
    setTemplate: function ( template ) {
        this.set( 'rendered', false );
        this.partials.template = template;
        this.set( 'rendered', true );
    },
    data: { rendered: true }
});

var ractive = new Ractive({
    el: 'main',
    template: '#template',
    data: { name: 'world' }
});

ractive.on( 'updateTemplate', function () {
    this.setTemplate( document.getElementById( 'newTemplate' ).innerHTML );
});



function extractTemplate ( string ) {
    if ( string.charAt( 0 ) === '#' ) {
        return document.getElementById( string.substring( 1 ) ).innerHTML;
    }
    
    return string;
}