Last Updated: February 25, 2016
·
649
· kayue

Tweet-sized templating engine

function t(s,d){
    for(var p in d) s=s.replace(new RegExp('{'+p+'}','g'), typeof d[p][1]=='f' ? d[p]() : d[p]);
    return s;
}

Usage

t("Hello {who}! It's {time} ms since epoch.", { who: "JavaScript", time: Date.now });
// "Hello JavaScript! It's 1299680443046 ms since epoch."

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside