diff --git a/examples/two_in_a_row.html b/examples/two_in_a_row.html new file mode 100644 index 0000000..dc38602 --- /dev/null +++ b/examples/two_in_a_row.html @@ -0,0 +1 @@ +{{greeting}}, {{name}}! \ No newline at end of file diff --git a/examples/two_in_a_row.js b/examples/two_in_a_row.js new file mode 100644 index 0000000..09c1809 --- /dev/null +++ b/examples/two_in_a_row.js @@ -0,0 +1,4 @@ +var two_in_a_row = { + name: "Joe", + greeting: "Welcome" +}; diff --git a/examples/two_in_a_row.txt b/examples/two_in_a_row.txt new file mode 100644 index 0000000..c6d6a9b --- /dev/null +++ b/examples/two_in_a_row.txt @@ -0,0 +1 @@ +Welcome, Joe! diff --git a/mustache.js b/mustache.js index a155779..6618961 100644 --- a/mustache.js +++ b/mustache.js @@ -84,44 +84,40 @@ var Mustache = function() { Replace {{foo}} and friends with values from our view */ render_tags: function(template, context) { - var pop_first = function(lines) { - var lines_array = lines.split("\n"); - lines_array.shift(); - return lines_array.join("\n"); - }; - + var lines = template.split("\n"); + var new_regex = function() { return new RegExp(that.otag + - "(=|!|<|\\{)?([^\/#]+?)\\1?" + that.ctag + "+", "m"); + "(=|!|<|\\{)?([^\/#]+?)\\1?" + + that.ctag + "+", + "g"); }; // tit for tat var that = this; - regex = new_regex(); - var lines = template; - - // for each {{(!<{)?foo}} tag, do... - while(regex.test(lines)) { - template = template.replace(regex, function (match, operator, name) { - switch(operator) { - case "!": // ignore comments - return match; - case "=": // set new delimiters - that.set_delimiters(name); - return ""; - case "<": // render partial - return that.render_partial(name, context); - case "{": // the triple mustache is unescaped - return that.find(name, context); - default: // escape the value - return that.escape(that.find(name, context)); - } - }, this); - regex = new_regex(); - lines = pop_first(lines); - } - return template; + var regex = new_regex(); + for (var i=0; i < lines.length; i++) { + lines[i] = lines[i].replace(regex, + function (match,operator,name) { + switch(operator) { + case "!": // ignore comments + return match; + case "=": // set new delimiters, rebuild the replace regexp + that.set_delimiters(name); + regex = new_regex(); + return ""; + case "<": // render partial + return that.render_partial(name, context); + case "{": // the triple mustache is unescaped + return that.find(name, context); + default: // escape the value + return that.escape(that.find(name, context)); + }}, + this); + }; + + return lines.join("\n"); }, set_delimiters: function(delimiters) {