Refactor template init logic
This commit is contained in:
+27
-23
@@ -70,13 +70,6 @@ export function template(templateSpec, env) {
|
||||
},
|
||||
programWithDepth: env.VM.programWithDepth,
|
||||
|
||||
initData: function(context, data) {
|
||||
if (!data || !('root' in data)) {
|
||||
data = data ? createFrame(data) : {};
|
||||
data.root = context;
|
||||
}
|
||||
return data;
|
||||
},
|
||||
data: function(data, depth) {
|
||||
while (data && depth--) {
|
||||
data = data._parent;
|
||||
@@ -99,28 +92,31 @@ export function template(templateSpec, env) {
|
||||
|
||||
var ret = function(context, options) {
|
||||
options = options || {};
|
||||
var namespace = options.partial ? options : env,
|
||||
helpers,
|
||||
var helpers,
|
||||
partials,
|
||||
data = options.data;
|
||||
|
||||
if (!options.partial) {
|
||||
helpers = container.helpers = container.merge(options.helpers, namespace.helpers);
|
||||
|
||||
if (templateSpec.usePartial) {
|
||||
partials = container.partials = container.merge(options.partials, namespace.partials);
|
||||
}
|
||||
if (templateSpec.useData) {
|
||||
data = container.initData(context, data);
|
||||
}
|
||||
} else {
|
||||
helpers = container.helpers = options.helpers;
|
||||
partials = container.partials = options.partials;
|
||||
ret._setup(options);
|
||||
if (!options.partial && templateSpec.useData) {
|
||||
data = initData(context, data);
|
||||
}
|
||||
return templateSpec.main.call(container, context, helpers, partials, data);
|
||||
return templateSpec.main.call(container, context, container.helpers, container.partials, data);
|
||||
};
|
||||
|
||||
ret.child = function(i) {
|
||||
ret._setup = function(options) {
|
||||
if (!options.partial) {
|
||||
container.helpers = container.merge(options.helpers, env.helpers);
|
||||
|
||||
if (templateSpec.usePartial) {
|
||||
container.partials = container.merge(options.partials, env.partials);
|
||||
}
|
||||
} else {
|
||||
container.helpers = options.helpers;
|
||||
container.partials = options.partials;
|
||||
}
|
||||
};
|
||||
|
||||
ret._child = function(i) {
|
||||
return container.programWithDepth(i);
|
||||
};
|
||||
return ret;
|
||||
@@ -164,3 +160,11 @@ export function invokePartial(partial, name, context, helpers, partials, data) {
|
||||
}
|
||||
|
||||
export function noop() { return ""; }
|
||||
|
||||
function initData(context, data) {
|
||||
if (!data || !('root' in data)) {
|
||||
data = data ? createFrame(data) : {};
|
||||
data.root = context;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user