Created
March 8, 2011 12:52
-
-
Save akorchev/860240 to your computer and use it in GitHub Desktop.
This file contains hidden or 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() { | |
this.tmpl3 = function tmpl(str, data) { | |
var value = "var out = ''; out+=" + "'" + | |
str.replace(/[\r\t\n]/g, " ") | |
.replace(/'(?=[^%]*%>)/g,"\t") | |
.split("'").join("\\'") | |
.split("\t").join("'") | |
.replace(/<%=(.+?)%>/g, "'; out += $1; out += '") | |
.split("<%").join("';") | |
.split("%>").join("out+='") | |
+ "'; return out;"; | |
return new Function("data", value); | |
} | |
})(); |
It would be nice to have some comments explaining the lines... I've been playing around with this gist and (in a very extreme case of a overly complex template), I had a problem with a string containing the % sign inside the script part of a template. I could get it to work by changing the line 6 to:
.replace(/'(?=[^]*%>)/g,"\t")
(Note that I just removed the % sign), but I have no clue how it may affect other templates. Apparently nothing else broke. What exactly does this line do?
Cheers!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey! Great template engine! More faster than original jresig micro-templating.