Bläddra i källkod

whitespace & reorder

tags/0.2
Jan Lehnardt 16 år sedan
förälder
incheckning
638094e9ae
1 ändrade filer med 46 tillägg och 51 borttagningar
  1. +46
    -51
      mustache.js

+ 46
- 51
mustache.js Visa fil

@@ -10,11 +10,11 @@

var Mustache = function() {
var Renderer = function() {};
Renderer.prototype = {
otag: "{{",
ctag: "}}",
render: function(template, context) {
// fail fast
if(template.indexOf(this.otag) == -1) {
@@ -25,18 +25,6 @@ var Mustache = function() {
return this.render_tags(html, context);
},

create_context: function(_context) {
if(this.is_object(_context)) {
return _context;
} else {
return {'.': _context};
}
},

is_object: function(a) {
return a && typeof a == 'object'
},

/*
Tries to find a partial in the global scope and render it
*/
@@ -67,18 +55,17 @@ var Mustache = function() {

// for each {{#foo}}{{/foo}} section do...
return template.replace(regex, function(match, name, content) {
var value = that.find(name, context);
if(that.is_array(value)) { // Enumerable, Let's loop!
return value.map(function(row) {
return that.render(content, that.merge(context, that.create_context(row)));
}).join('');
} else if(value) { // boolean section
return that.render(content, context);
} else {
return "";
}
var value = that.find(name, context);
if(that.is_array(value)) { // Enumerable, Let's loop!
return value.map(function(row) {
return that.render(content, that.merge(context, that.create_context(row)));
}).join('');
} else if(value) { // boolean section
return that.render(content, context);
} else {
return "";
}
);
});
},

/*
@@ -86,12 +73,10 @@ var Mustache = function() {
*/
render_tags: function(template, context) {
var lines = template.split("\n");
var new_regex = function() {
return new RegExp(that.otag +
"(=|!|<|\\{)?([^\/#]+?)\\1?" +
that.ctag + "+",
"g");
return new RegExp(that.otag + "(=|!|<|\\{)?([^\/#]+?)\\1?" +
that.ctag + "+", "g");
};

// tit for tat
@@ -99,27 +84,25 @@ var Mustache = function() {

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();
// redo the line in order to get tags with the new delimiters on the same line
i--;
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);
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();
// redo the line in order to get tags with the new delimiters on the same line
i--;
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");
},

@@ -177,7 +160,7 @@ var Mustache = function() {
},

/*
Merges all properties of object `b` into object `a`.
Merges all properties of object `b` into object `a`.
`b.property` overwrites a.property`
*/
merge: function(a, b) {
@@ -195,6 +178,18 @@ var Mustache = function() {
return _new;
},

create_context: function(_context) {
if(this.is_object(_context)) {
return _context;
} else {
return {'.': _context};
}
},

is_object: function(a) {
return a && typeof a == 'object'
},

/*
Thanks Doug Crockford
JavaScript — The Good Parts lists an alternative that works better with


Laddar…
Avbryt
Spara