Skip to content

Instantly share code, notes, and snippets.

@akorchev
Created March 8, 2011 12:52
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save akorchev/860240 to your computer and use it in GitHub Desktop.
Save akorchev/860240 to your computer and use it in GitHub Desktop.
(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);
}
})();
@henriquegogo
Copy link

Hey! Great template engine! More faster than original jresig micro-templating.

@gersongoulart
Copy link

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