Merge pull request #848 from wycats/recursive-partial

Implement partial recursive lookup
This commit is contained in:
Kevin Decker
2014-08-14 00:48:01 -05:00
3 changed files with 31 additions and 10 deletions
@@ -123,7 +123,10 @@ JavaScriptCompiler.prototype = {
ret.useData = true;
}
if (this.useDepths) {
ret.depths = true;
ret.useDepths = true;
}
if (this.options.compat) {
ret.compat = true;
}
if (!asObject) {
@@ -366,7 +369,7 @@ JavaScriptCompiler.prototype = {
var i = 0,
len = parts.length;
if (!scoped && this.isChild && this.options.compat && !this.lastContext) {
if (!scoped && this.options.compat && !this.lastContext) {
// The depthed query is expected to handle the undefined logic for the root level that
// is implemented below, so we evaluate that directly in compat mode
this.pushStackLiteral(this.depthedLookup(parts[i++]));
@@ -616,6 +619,9 @@ JavaScriptCompiler.prototype = {
if (this.options.data) {
params.push("data");
}
if (this.options.compat) {
params.push('depths');
}
this.push("this.invokePartial(" + params.join(", ") + ")");
},
+8 -8
View File
@@ -31,16 +31,16 @@ export function template(templateSpec, env) {
// for external users to override these as psuedo-supported APIs.
env.VM.checkRevision(templateSpec.compiler);
var invokePartialWrapper = function(partial, indent, name, context, hash, helpers, partials, data) {
var invokePartialWrapper = function(partial, indent, name, context, hash, helpers, partials, data, depths) {
if (hash) {
context = Utils.extend({}, context, hash);
}
var result = env.VM.invokePartial.call(this, partial, name, context, helpers, partials, data);
var result = env.VM.invokePartial.call(this, partial, name, context, helpers, partials, data, depths);
if (result == null && env.compile) {
var options = { helpers: helpers, partials: partials, data: data };
partials[name] = env.compile(partial, { data: data !== undefined }, env);
var options = { helpers: helpers, partials: partials, data: data, depths: depths };
partials[name] = env.compile(partial, { data: data !== undefined, compat: templateSpec.compat }, env);
result = partials[name](context, options);
}
if (result != null) {
@@ -125,8 +125,8 @@ export function template(templateSpec, env) {
data = initData(context, data);
}
var depths;
if (templateSpec.depths) {
depths = [context];
if (templateSpec.useDepths) {
depths = options.depths ? [context].concat(options.depths) : [context];
}
return templateSpec.main.call(container, context, container.helpers, container.partials, data, depths);
@@ -166,8 +166,8 @@ export function program(container, i, fn, data, depths) {
return prog;
}
export function invokePartial(partial, name, context, helpers, partials, data) {
var options = { partial: true, helpers: helpers, partials: partials, data: data };
export function invokePartial(partial, name, context, helpers, partials, data, depths) {
var options = { partial: true, helpers: helpers, partials: partials, data: data, depths: depths };
if(partial === undefined) {
throw new Exception("The partial " + name + " could not be found");
+15
View File
@@ -155,4 +155,19 @@ describe('partials', function() {
"Dudes:\n Yehuda\n http://yehuda!\n Alan\n http://alan!\n");
});
});
describe('compat mode', function() {
it('partials can access parents', function() {
var string = 'Dudes: {{#dudes}}{{> dude}}{{/dudes}}';
var partial = '{{name}} ({{url}}) {{root}} ';
var hash = {root: 'yes', dudes: [{name: 'Yehuda', url: 'http://yehuda'}, {name: 'Alan', url: 'http://alan'}]};
shouldCompileToWithPartials(string, [hash, {}, {dude: partial}, true], true, 'Dudes: Yehuda (http://yehuda) yes Alan (http://alan) yes ');
});
it('partials inherit compat', function() {
var string = 'Dudes: {{> dude}}';
var partial = '{{#dudes}}{{name}} ({{url}}) {{root}} {{/dudes}}';
var hash = {root: 'yes', dudes: [{name: 'Yehuda', url: 'http://yehuda'}, {name: 'Alan', url: 'http://alan'}]};
shouldCompileToWithPartials(string, [hash, {}, {dude: partial}, true], true, 'Dudes: Yehuda (http://yehuda) yes Alan (http://alan) yes ');
});
});
});