Skip to content

Instantly share code, notes, and snippets.

@caiotarifa
Created September 2, 2015 19:09
Show Gist options
  • Save caiotarifa/7fd4c4cd1b9c0c0983c6 to your computer and use it in GitHub Desktop.
Save caiotarifa/7fd4c4cd1b9c0c0983c6 to your computer and use it in GitHub Desktop.
Lightweight JavaScript Templating Engine
var Template = (function (selector) {
'use strict';
function render(data) {
var node = this.node;
if (data) {
for (var variable in data) {
node = node.split('{{' + variable + '}}').join(data[variable]);
}
}
return node;
}
function compile() {
return {
node: document.querySelector(selector).innerHTML.trim(),
render: render
}
}
return { compile: compile }
});

Lightweight JavaScript Templating Engine

Omg, it's mustache-like.

Usage

<script class="example" type="text/html">
  <p>Follow <a href="http://twitter.com/{{twitter}}">{{name}}</a> on Twitter.</p>
</script>
var context, template;

context = {
  name: 'Caio Tarifa',
  twitter: 'caiotarifa'
};

template = Template('.example').compile();
template = template.render(context)

document.body.innerHTML = template;

Would you like to see a Codepen Demo?

:D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment