Skip to content

Instantly share code, notes, and snippets.

@m1el
Created November 7, 2013 11:01
Show Gist options
  • Save m1el/7352856 to your computer and use it in GitHub Desktop.
Save m1el/7352856 to your computer and use it in GitHub Desktop.
a slightly modified rnd template engine
function htmlEscape(str) {
var set = {
'&': '&',
'<': '&lt;',
'>': '&gt;',
"'": '&#39;',
'"': '&quot;'
};
return ('' + str).replace(/[&<>"']/g, function (char) {
return set[char];
});
}
function RND(tmpl, ns, safe) {
safe = safe || {};
var fn = function (w, g) {
var res = safe[g] ? ns[g] : htmlEscape(ns[g]);
return typeof res !== 'undefined' ? res : w;
};
return tmpl.replace(/\$\{([A-Za-z0-9_|.]+)\}/g, fn);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment