* Remove legacy support for inverse sections as additional parameters.
* Unify inverse and normal block helpers * Make Handlebars.Exception inherit from JS Error
This commit is contained in:
+16
-12
@@ -36,8 +36,9 @@ Handlebars.registerHelper('helperMissing', function(arg) {
|
||||
}
|
||||
});
|
||||
|
||||
Handlebars.registerHelper('blockHelperMissing', function(context, fn, inverse) {
|
||||
inverse = inverse || function() {};
|
||||
Handlebars.registerHelper('blockHelperMissing', function(context, options) {
|
||||
var inverse = options.inverse || function() {}, fn = options.fn;
|
||||
|
||||
|
||||
var ret = "";
|
||||
var type = Object.prototype.toString.call(context);
|
||||
@@ -62,11 +63,10 @@ Handlebars.registerHelper('blockHelperMissing', function(context, fn, inverse) {
|
||||
} else {
|
||||
return fn(context);
|
||||
}
|
||||
}, function(context, fn) {
|
||||
return fn(context);
|
||||
});
|
||||
|
||||
Handlebars.registerHelper('each', function(context, fn, inverse) {
|
||||
Handlebars.registerHelper('each', function(context, options) {
|
||||
var fn = options.fn, inverse = options.inverse;
|
||||
var ret = "";
|
||||
|
||||
if(context && context.length > 0) {
|
||||
@@ -79,20 +79,24 @@ Handlebars.registerHelper('each', function(context, fn, inverse) {
|
||||
return ret;
|
||||
});
|
||||
|
||||
Handlebars.registerHelper('if', function(context, fn, inverse) {
|
||||
Handlebars.registerHelper('if', function(context, options) {
|
||||
if(!context || Handlebars.Utils.isEmpty(context)) {
|
||||
return inverse(this);
|
||||
return options.inverse(this);
|
||||
} else {
|
||||
return fn(this);
|
||||
return options.fn(this);
|
||||
}
|
||||
});
|
||||
|
||||
Handlebars.registerHelper('unless', function(context, fn, inverse) {
|
||||
return Handlebars.helpers['if'].call(this, context, inverse, fn);
|
||||
Handlebars.registerHelper('unless', function(context, options) {
|
||||
var fn = options.fn, inverse = options.inverse;
|
||||
options.fn = inverse;
|
||||
options.inverse = fn;
|
||||
|
||||
return Handlebars.helpers['if'].call(this, context, options);
|
||||
});
|
||||
|
||||
Handlebars.registerHelper('with', function(context, fn) {
|
||||
return fn(context);
|
||||
Handlebars.registerHelper('with', function(context, options) {
|
||||
return options.fn(context);
|
||||
});
|
||||
|
||||
Handlebars.logger = {
|
||||
|
||||
+10
-25
@@ -19,7 +19,6 @@ Handlebars.JavaScriptCompiler = function() {};
|
||||
invokeProgram: 11,
|
||||
invokePartial: 12,
|
||||
push: 13,
|
||||
invokeInverse: 14,
|
||||
assignToHash: 15,
|
||||
pushStringParam: 16
|
||||
};
|
||||
@@ -36,7 +35,6 @@ Handlebars.JavaScriptCompiler = function() {};
|
||||
invokeProgram: 2,
|
||||
invokePartial: 1,
|
||||
push: 1,
|
||||
invokeInverse: 1,
|
||||
assignToHash: 1,
|
||||
pushStringParam: 1
|
||||
};
|
||||
@@ -157,10 +155,13 @@ Handlebars.JavaScriptCompiler = function() {};
|
||||
},
|
||||
|
||||
inverse: function(block) {
|
||||
this.ID(block.mustache.id);
|
||||
var params = this.setupStackForMustache(block.mustache);
|
||||
|
||||
var programGuid = this.compileProgram(block.program);
|
||||
|
||||
this.opcode('invokeInverse', programGuid);
|
||||
this.declare('inverse', programGuid);
|
||||
|
||||
this.opcode('invokeProgram', null, params.length);
|
||||
this.opcode('append');
|
||||
},
|
||||
|
||||
@@ -438,15 +439,11 @@ Handlebars.JavaScriptCompiler = function() {};
|
||||
container.children = this.environment.children;
|
||||
|
||||
return function(context, options, $depth) {
|
||||
try {
|
||||
options = options || {};
|
||||
var args = [Handlebars, context, options.helpers, options.partials, options.data];
|
||||
var depth = Array.prototype.slice.call(arguments, 2);
|
||||
args = args.concat(depth);
|
||||
return container.render.apply(container, args);
|
||||
} catch(e) {
|
||||
throw e;
|
||||
}
|
||||
options = options || {};
|
||||
var args = [Handlebars, context, options.helpers, options.partials, options.data];
|
||||
var depth = Array.prototype.slice.call(arguments, 2);
|
||||
args = args.concat(depth);
|
||||
return container.render.apply(container, args);
|
||||
};
|
||||
},
|
||||
|
||||
@@ -565,11 +562,6 @@ Handlebars.JavaScriptCompiler = function() {};
|
||||
|
||||
params.push('tmp1');
|
||||
|
||||
// TODO: This is legacy behavior. Deprecate and remove.
|
||||
if(inverse) {
|
||||
params.push(inverse);
|
||||
}
|
||||
|
||||
this.populateCall(params, id, helperId || id, fn);
|
||||
},
|
||||
|
||||
@@ -583,13 +575,6 @@ Handlebars.JavaScriptCompiler = function() {};
|
||||
fn.call(this, nextStack, helperMissingString, id);
|
||||
},
|
||||
|
||||
invokeInverse: function(guid) {
|
||||
var program = this.programExpression(guid);
|
||||
|
||||
var blockMissingParams = ["context", this.topStack(), "this.noop", program];
|
||||
this.pushStack("helpers.blockHelperMissing.call(" + blockMissingParams.join(", ") + ")");
|
||||
},
|
||||
|
||||
invokePartial: function(context) {
|
||||
this.pushStack("this.invokePartial(" + this.nameLookup('partials', context, 'partial') + ", '" + context + "', " + this.popStack() + ", helpers, partials);");
|
||||
},
|
||||
|
||||
@@ -2,8 +2,13 @@ var Handlebars = require("handlebars");
|
||||
|
||||
// BEGIN(BROWSER)
|
||||
Handlebars.Exception = function(message) {
|
||||
this.message = message;
|
||||
var tmp = Error.prototype.constructor.apply(this, arguments);
|
||||
|
||||
for (var p in tmp) {
|
||||
if (tmp.hasOwnProperty(p)) { this[p] = tmp[p]; }
|
||||
}
|
||||
};
|
||||
Handlebars.Exception.prototype = new Error;
|
||||
|
||||
// Build out our basic SafeString type
|
||||
Handlebars.SafeString = function(string) {
|
||||
|
||||
+4
-10
@@ -179,12 +179,6 @@ test("inverted section with empty set", function() {
|
||||
shouldCompileTo(string, hash, "Right On!", "Inverted section rendered when value is empty set.");
|
||||
});
|
||||
|
||||
test("inverted section using result of function call", function() {
|
||||
var string = "{{goodbyes}}{{^goodbyes}}Right On!{{/goodbyes}}";
|
||||
var hash = {goodbyes: function() { return false; }}
|
||||
shouldCompileTo(string, hash, "Right On!", "Inverted section rendered when result of function in expression is false.");
|
||||
});
|
||||
|
||||
module("blocks");
|
||||
|
||||
test("array", function() {
|
||||
@@ -341,25 +335,25 @@ test("block inverted sections with empty arrays", function() {
|
||||
|
||||
test("block helper inverted sections", function() {
|
||||
var string = "{{#list people}}{{name}}{{^}}<em>Nobody's here</em>{{/list}}"
|
||||
var list = function(context, fn, inverse) {
|
||||
var list = function(context, options) {
|
||||
if (context.length > 0) {
|
||||
var out = "<ul>";
|
||||
for(var i = 0,j=context.length; i < j; i++) {
|
||||
out += "<li>";
|
||||
out += fn(context[i]);
|
||||
out += options.fn(context[i]);
|
||||
out += "</li>";
|
||||
}
|
||||
out += "</ul>";
|
||||
return out;
|
||||
} else {
|
||||
return "<p>" + inverse(this) + "</p>";
|
||||
return "<p>" + options.inverse(this) + "</p>";
|
||||
}
|
||||
};
|
||||
|
||||
var hash = {list: list, people: [{name: "Alan"}, {name: "Yehuda"}]};
|
||||
var empty = {list: list, people: []};
|
||||
var rootMessage = {
|
||||
list: function(context, fn, inverse) { if(context.length === 0) { return "<p>" + inverse(this) + "</p>"; } },
|
||||
list: function(context, options) { if(context.length === 0) { return "<p>" + options.inverse(this) + "</p>"; } },
|
||||
people: [],
|
||||
message: "Nobody's here"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user