Allow passing depths to _child

This commit is contained in:
kpdecker
2014-08-25 00:32:57 -05:00
parent 477a26913a
commit 501a640330
3 changed files with 15 additions and 8 deletions
+2 -2
View File
@@ -426,11 +426,11 @@ export function compile(input, options, env) {
}
return compiled._setup(options);
};
ret._child = function(i) {
ret._child = function(i, data, depths) {
if (!compiled) {
compiled = compileInput();
}
return compiled._child(i);
return compiled._child(i, data, depths);
};
return ret;
}
+4 -4
View File
@@ -149,12 +149,12 @@ export function template(templateSpec, env) {
}
};
ret._child = function(i) {
if (templateSpec.useDepths) {
throw new Exception('_child can not be used with depthed templates');
ret._child = function(i, data, depths) {
if (templateSpec.useDepths && !depths) {
throw new Exception('must pass parent depths');
}
return program(container, i, templateSpec[i]);
return program(container, i, templateSpec[i], data, depths);
};
return ret;
}
+9 -2
View File
@@ -39,18 +39,25 @@ describe('runtime', function() {
return;
}
it('should throw for depthed methods', function() {
it('should throw for depthed methods without depths', function() {
shouldThrow(function() {
var template = Handlebars.compile('{{#foo}}{{../bar}}{{/foo}}');
// Calling twice to hit the non-compiled case.
template._setup({});
template._setup({});
template._child(1);
}, Error, '_child can not be used with depthed templates');
}, Error, 'must pass parent depths');
});
it('should expose child template', function() {
var template = Handlebars.compile('{{#foo}}bar{{/foo}}');
// Calling twice to hit the non-compiled case.
equal(template._child(1)(), 'bar');
equal(template._child(1)(), 'bar');
});
it('should render depthed content', function() {
var template = Handlebars.compile('{{#foo}}{{../bar}}{{/foo}}');
// Calling twice to hit the non-compiled case.
equal(template._child(1, undefined, [{bar: 'baz'}])(), 'baz');
});
});
});