Implement partial recursive lookup

This commit is contained in:
kpdecker
2014-08-14 00:28:57 -05:00
parent 0edce6e1d1
commit 9f8110fe15
3 changed files with 16 additions and 7 deletions
@@ -369,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++]));
@@ -619,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(", ") + ")");
},
+6 -6
View File
@@ -31,15 +31,15 @@ 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 };
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);
}
@@ -126,7 +126,7 @@ export function template(templateSpec, env) {
}
var depths;
if (templateSpec.useDepths) {
depths = [context];
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");
+6
View File
@@ -157,6 +157,12 @@ describe('partials', function() {
});
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}}';