Преглед изворни кода

Add parameterized function support.

tags/0.5.0-vsc
thegrandpoobah пре 16 година
родитељ
комит
7c8b513db9
1 измењених фајлова са 20 додато и 4 уклоњено
  1. +20
    -4
      mustache.js

+ 20
- 4
mustache.js Прегледај датотеку

@@ -104,12 +104,13 @@ var Mustache = function() {


var that = this; var that = this;
// CSW - Added "+?" so it finds the tighest bound, not the widest // CSW - Added "+?" so it finds the tighest bound, not the widest
var regex = new RegExp(this.otag + "(\\^|\\#)\\s*(.+)\\s*" + this.ctag +
"\\s*([\\s\\S]+?)" + this.otag + "\\/\\s*\\2\\s*" + this.ctag +
var regex = new RegExp(this.otag + "(\\^|\\#)\\s*((.+?)(\\(.*\\))?)\\s*" + this.ctag +
"\\s*([\\s\\S]+?)" + this.otag + "\\/\\s*\\3\\s*" + this.ctag +
"\\s*", "mg"); "\\s*", "mg");


// for each {{#foo}}{{/foo}} section do... // for each {{#foo}}{{/foo}} section do...
return template.replace(regex, function(match, type, name, content) {
return template.replace(regex, function(match, type, name, reserved1, reserved2, content) {
// the reserved variables are not being used
var value = that.find(name, context); var value = that.find(name, context);
if(type == "^") { // inverted section if(type == "^") { // inverted section
if(!value || that.is_array(value) && value.length === 0) { if(!value || that.is_array(value) && value.length === 0) {
@@ -221,13 +222,28 @@ var Mustache = function() {
} else if(is_kinda_truthy(this.context[name])) { } else if(is_kinda_truthy(this.context[name])) {
value = this.context[name]; value = this.context[name];
} }

if(typeof value === "function") { if(typeof value === "function") {
return value.apply(context); return value.apply(context);
} }
if(value !== undefined) { if(value !== undefined) {
return value; return value;
} }
if (value === undefined && name.indexOf('(') != -1) {
// if value turned out to not exist on the context, then check to see if the function is parameterized
var matches = name.match(/(.+)\((.+,?)+\)/);

if (matches.length === 3) {
name = matches[1];
value = this.context[name];

if (typeof value === "function" && matches[2]) {
var that = this;
var args = this.map(matches[2].split(/\s*,/),
function(ele) { return that.find(ele, that.context); });
return value.apply(context, args);
}
}
}
// silently ignore unkown variables // silently ignore unkown variables
return ""; return "";
}, },


Loading…
Откажи
Сачувај