From c8c378c4f58360bd9c7c27910b020625ef6612ed Mon Sep 17 00:00:00 2001 From: Sebastian Cohnen Date: Sun, 18 Oct 2009 01:22:11 +0100 Subject: [PATCH] Rewritten render_tags, added test for two placeholder in one row --- examples/two_in_a_row.html | 1 + examples/two_in_a_row.js | 4 +++ examples/two_in_a_row.txt | 1 + mustache.js | 60 ++++++++++++++++++-------------------- 4 files changed, 34 insertions(+), 32 deletions(-) create mode 100644 examples/two_in_a_row.html create mode 100644 examples/two_in_a_row.js create mode 100644 examples/two_in_a_row.txt 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 8dcecb6..bdbe445 100644 --- a/mustache.js +++ b/mustache.js @@ -95,44 +95,40 @@ var Mustache = function() { Replace {{foo}} and friends with values from our view */ render_tags: function(template) { - var pop_first = function(lines) { - var lines_array = lines.split("\n"); - lines_array.shift(); - return lines_array.join("\n"); - }; - - var new_regex = function() { + var lines = template.split("\n"); + + 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); - case "{": // the triple mustache is unescaped - return that.find(name); - default: // escape the value - return that.escape(that.find(name)); - } - }, 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); + case "{": // the triple mustache is unescaped + return that.find(name); + default: // escape the value + return that.escape(that.find(name)); + }}, + this); + }; + + return lines.join("\n"); }, set_delimiters: function(delimiters) {