Skip to content

Instantly share code, notes, and snippets.

@soska
Created September 13, 2010 21:53
Show Gist options
  • Save soska/578122 to your computer and use it in GitHub Desktop.
Save soska/578122 to your computer and use it in GitHub Desktop.
var post = {
title: "Shave : Ultra simple Javascript templates",
author: "Armando Sosa",
category: "self-importance",
permalink: "http://startupinmexico.com/?p=8"
}
var template = "<li><a href='{{permalink}}'>{{title}}</a> by {{author}} on {{category}}</li>";
var html = shave(template, post);
// really simple template engine for {{mustache}} style templates.
shave = function(str,obj){
var rex = /\{\{[^\s]+\}\}/g;
var matches = str.match(rex);
for (var i = matches.length - 1; i >= 0; i--){
var key = matches[i].replace(/\{\{|\}\}/g,'');
if (typeof(obj[key]) !== "undefined") {
str = str.replace(matches[i],obj[key]);
};
};
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment