Skip to content

Instantly share code, notes, and snippets.

@Rplus
Last active December 28, 2015 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rplus/7559318 to your computer and use it in GitHub Desktop.
Save Rplus/7559318 to your computer and use it in GitHub Desktop.
自幹型tpl似乎還需要調整
// escape unsafety chart
// via: http://stackoverflow.com/a/12034334
window.entityMap = {
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
'\'': '&#39;',
'/': '&#x2F;'
};
// 將 str(template) 中特定字串(#{$str})以 物件式 批次取代傳回
window.replaceTpl = function(str, obj) {
var pattern = /\#\{\$(.+?)\}/g;
return str.replace(pattern, function(match) {
return String(obj[match.replace(pattern, '$1')]).replace(/[<>"'\/]/g, function(s) {
return window.entityMap[s];
});
});
};
@Rplus
Copy link
Author

Rplus commented Aug 24, 2014

加了 escape 的 string map

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment