« Back to Index

@madrobby’s 140 bytes template engine

View original Gist on GitHub

Tags: #js

template.js

function template(string, data, prop) {
    for (prop in data) {
        string = string.replace(new RegExp('{' + prop + '}', 'g'), data[prop]);
    }
    return string;
}

/*
String templating engine:

t("Hello {name}!, It is {date}!", { name: "Thomas", date: function(){ return new Date }});
// = "Hello Thomas!, It is Sun May 08 2011 15:15:33 GMT-0400 (EDT)!"
*/