Created
November 7, 2013 11:01
-
-
Save m1el/7352856 to your computer and use it in GitHub Desktop.
a slightly modified rnd template engine
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
function htmlEscape(str) { | |
var set = { | |
'&': '&', | |
'<': '<', | |
'>': '>', | |
"'": ''', | |
'"': '"' | |
}; | |
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