Implement block decorators

These allow for a given block to be wrapped in helper methods or metadata and allow for more control over the current container and method before the code is run.
This commit is contained in:
kpdecker
2015-08-19 08:27:13 -07:00
parent 408192ba9f
commit 452afbf2ff
7 changed files with 244 additions and 8 deletions
+21 -2
View File
@@ -90,7 +90,9 @@ export function template(templateSpec, env) {
invokePartial: invokePartialWrapper,
fn: function(i) {
return templateSpec[i];
let ret = templateSpec[i];
ret.decorator = templateSpec[i + '_d'];
return ret;
},
programs: [],
@@ -142,7 +144,17 @@ export function template(templateSpec, env) {
}
}
return '' + templateSpec.main(container, context, container.helpers, container.partials, data, blockParams, depths);
function main(context/*, options*/) {
return '' + templateSpec.main(container, context, container.helpers, container.partials, data, blockParams, depths);
}
if (templateSpec.main_d) {
// Note that we are ignoring the props value here as we apply things slightly differently
// when applying decorators to the root function.
main = templateSpec.main_d(main, {}, container, undefined, data, blockParams, depths);
}
return main(context, options);
}
ret.isTop = true;
@@ -190,6 +202,13 @@ export function wrapProgram(container, i, fn, data, declaredBlockParams, blockPa
blockParams && [options.blockParams].concat(blockParams),
currentDepths);
}
if (fn.decorator) {
let props = {};
prog = fn.decorator(prog, props, container, depths && depths[0], data, blockParams, depths);
Utils.extend(prog, props);
}
prog.program = i;
prog.depth = depths ? depths.length : 0;
prog.blockParams = declaredBlockParams || 0;