Created
June 20, 2012 10:23
-
-
Save bjjay/2959242 to your computer and use it in GitHub Desktop.
javascript template
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
// By default, Underscore uses ERB-style template delimiters, change the | |
// following template settings to use alternative delimiters. | |
_.templateSettings = { | |
evaluate : /<%([\s\S]+?)%>/g, | |
interpolate : /<%=([\s\S]+?)%>/g, | |
escape : /<%-([\s\S]+?)%>/g | |
}; | |
// JavaScript micro-templating, similar to John Resig's implementation. | |
// Underscore templating handles arbitrary delimiters, preserves whitespace, | |
// and correctly escapes quotes within interpolated code. | |
_.template = function(str, data) { | |
var c = _.templateSettings; | |
var tmpl = 'var __p=[],print=function(){__p.push.apply(__p,arguments);};' + | |
'with(obj||{}){__p.push(\'' + | |
str.replace(/\\/g, '\\\\') | |
.replace(/'/g, "\\'") | |
.replace(c.escape, function(match, code) { | |
return "',_.escape(" + code.replace(/\\'/g, "'") + "),'"; | |
}) | |
.replace(c.interpolate, function(match, code) { | |
return "'," + code.replace(/\\'/g, "'") + ",'"; | |
}) | |
.replace(c.evaluate || null, function(match, code) { | |
return "');" + code.replace(/\\'/g, "'") | |
.replace(/[\r\n\t]/g, ' ') + "__p.push('"; | |
}) | |
.replace(/\r/g, '\\r') | |
.replace(/\n/g, '\\n') | |
.replace(/\t/g, '\\t') | |
+ "');}return __p.join('');"; | |
var func = new Function('obj', tmpl); | |
return data ? func(data) : func; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment