Created
September 13, 2010 21:53
-
-
Save soska/578122 to your computer and use it in GitHub Desktop.
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
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); |
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
// 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