summaryrefslogtreecommitdiffstats
path: root/lib/handlebars/runtime.js
diff options
context:
space:
mode:
authorwycats <wycats@gmail.com>2010-12-12 13:33:17 -0800
committerwycats <wycats@gmail.com>2010-12-12 13:33:17 -0800
commitec948c7382603b88a6e80ab949120cca2b64fb5f (patch)
tree9da2b52a3edebe7c2552653c488cc996302baf9f /lib/handlebars/runtime.js
parent57e72c9fd270401a36c9ad7aeb305896841404e5 (diff)
downloadhandlebars.js-ec948c7382603b88a6e80ab949120cca2b64fb5f.zip
handlebars.js-ec948c7382603b88a6e80ab949120cca2b64fb5f.tar.gz
handlebars.js-ec948c7382603b88a6e80ab949120cca2b64fb5f.tar.bz2
Small restructuring. Have I mentioned how much I hate having to write modules that work in both CommonJS and the browser?
Diffstat (limited to 'lib/handlebars/runtime.js')
-rw-r--r--lib/handlebars/runtime.js41
1 files changed, 20 insertions, 21 deletions
diff --git a/lib/handlebars/runtime.js b/lib/handlebars/runtime.js
index e091b99..d78f0d8 100644
--- a/lib/handlebars/runtime.js
+++ b/lib/handlebars/runtime.js
@@ -13,6 +13,7 @@ Handlebars.Runtime = require("handlebars/runtime").Runtime;
Handlebars.Utils = require("handlebars/utils").Utils;
Handlebars.SafeString = require("handlebars/utils").SafeString;
Handlebars.Exception = require("handlebars/utils").Exception;
+Handlebars.parse = require("handlebars/compiler").Handlebars.parse;
// BEGIN(BROWSER)
// A Context wraps data, and makes it possible to extract a
@@ -54,6 +55,8 @@ Handlebars.Context.prototype = {
context.data = context.data[parts[i]];
}
+ if(context.data !== undefined) { return context; }
+
if(parts.length === 1 && context.data === undefined) {
context.data = context.helpers[parts[0]];
}
@@ -70,15 +73,11 @@ Handlebars.proxy = function(obj) {
return new Proxy();
};
-Handlebars.Runtime = function(context, helpers, partials, stack) {
+Handlebars.Runtime = function(context, stack) {
this.stack = stack || [];
this.buffer = "";
- if(context && context.isContext) {
- this.context = context.clone();
- } else {
- this.context = new Handlebars.Context(context, helpers, partials)
- }
+ this.context = context;
};
Handlebars.Runtime.prototype = {
@@ -137,19 +136,19 @@ Handlebars.Runtime.prototype = {
},
block: function(block) {
- var mustache = block.mustache,
- id = mustache.id;
+ var mustache = block.mustache, data;
- var idObj = this.ID(mustache.id),
+ var id = mustache.id,
+ idObj = this.ID(mustache.id),
data = idObj.data;
var result;
- if(toString.call(data) !== "[object Function]") {
+ if(typeof data === "function") {
+ params = this.evaluateParams(mustache.params);
+ } else {
params = [data];
data = this.context.helpers.blockHelperMissing;
- } else {
- params = this.evaluateParams(mustache.params);
}
params.push(this.wrapProgram(block.program));
@@ -159,7 +158,7 @@ Handlebars.Runtime.prototype = {
if(block.program.inverse) {
params.pop();
params.push(this.wrapProgram(block.program.inverse));
- result = data.not.apply(this.wrapContext(), params);
+ result = data.not ? data.not.apply(this.wrapContext(), params) : "";
this.buffer = this.buffer + result;
}
},
@@ -187,7 +186,7 @@ Handlebars.Runtime.prototype = {
} else {
context = this.context;
}
- var runtime = new Handlebars.Runtime(context, null, null, this.stack.slice(0));
+ var runtime = new Handlebars.Runtime(context, this.stack);
this.buffer = this.buffer + runtime.program(program);
},
@@ -260,16 +259,15 @@ Handlebars.Runtime.prototype = {
},
wrapProgram: function(program) {
- var runtime = this,
- stack = this.stack.slice(0),
- helpers = this.context.helpers,
- partials = this.context.partials;
-
- stack.push(this.context);
+ var currentContext = this.context;
+ var stack = this.stack.slice(0);
return function(context) {
if(context && context.isWrappedContext) { context = context.__data__; }
- var runtime = new Handlebars.Runtime(context, helpers, partials, stack);
+
+ stack.push(currentContext);
+ var newContext = new Handlebars.Context(context, currentContext.helpers, currentContext.partials);
+ var runtime = new Handlebars.Runtime(newContext, stack);
runtime.program(program);
return runtime.buffer;
};
@@ -279,3 +277,4 @@ Handlebars.Runtime.prototype = {
// END(BROWSER)
exports.Runtime = Handlebars.Runtime;
+exports.Context = Handlebars.Context;