Bläddra i källkod

Rewritten render_tags, added test for two placeholder in one row

tags/0.2
Sebastian Cohnen 16 år sedan
förälder
incheckning
c8c378c4f5
4 ändrade filer med 34 tillägg och 32 borttagningar
  1. +1
    -0
      examples/two_in_a_row.html
  2. +4
    -0
      examples/two_in_a_row.js
  3. +1
    -0
      examples/two_in_a_row.txt
  4. +28
    -32
      mustache.js

+ 1
- 0
examples/two_in_a_row.html Visa fil

@@ -0,0 +1 @@
{{greeting}}, {{name}}!

+ 4
- 0
examples/two_in_a_row.js Visa fil

@@ -0,0 +1,4 @@
var two_in_a_row = {
name: "Joe",
greeting: "Welcome"
};

+ 1
- 0
examples/two_in_a_row.txt Visa fil

@@ -0,0 +1 @@
Welcome, Joe!

+ 28
- 32
mustache.js Visa fil

@@ -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) {


Laddar…
Avbryt
Spara