Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime.js): partials compile not caching #1600

Merged
merged 9 commits into from Nov 18, 2019
12 changes: 11 additions & 1 deletion lib/handlebars/runtime.js
Expand Up @@ -124,6 +124,15 @@ export function template(templateSpec, env) {
}
return value;
},
mergeIfNeeded: function(param, common) {
let obj = param || common;

if (param && common && (param !== common)) {
obj = Utils.extend({}, common, param);
}

return obj;
},
// An empty object to use as replacement for null-contexts
nullContext: Object.seal({}),

Expand Down Expand Up @@ -161,7 +170,8 @@ export function template(templateSpec, env) {
container.helpers = Utils.extend({}, env.helpers, options.helpers);

if (templateSpec.usePartial) {
container.partials = Utils.extend({}, env.partials, options.partials);
// Use mergeIfNeeded here to prevent compiling global partials multiple times
container.partials = container.mergeIfNeeded(options.partials, env.partials);
}
if (templateSpec.usePartial || templateSpec.useDecorators) {
container.decorators = Utils.extend({}, env.decorators, options.decorators);
Expand Down
25 changes: 25 additions & 0 deletions spec/compiler.js
Expand Up @@ -108,4 +108,29 @@ describe('compiler', function() {
equal(/return ""/.test(Handlebars.precompile('')), true);
});
});

describe('Compile Partials Correctly', function() {
ole-martin marked this conversation as resolved.
Show resolved Hide resolved
var compileCount = 0;
var origTemplateFunc = Handlebars.template;
before(function() {
Handlebars.template = function(params) {
ole-martin marked this conversation as resolved.
Show resolved Hide resolved
compileCount++;
return origTemplateFunc(params);
};
Handlebars.registerPartial({
'dude': 'I am a partial'
});
});
after(function() {
Handlebars.template = origTemplateFunc;
Handlebars.unregisterPartial('dude');
ole-martin marked this conversation as resolved.
Show resolved Hide resolved
});

it('should only compile global partials once', function() {
var string = 'Dudes: {{> dude}} {{> dude}}';
Handlebars.compile(string)(); // This should compile template + partial once
Handlebars.compile(string)(); // This should only compile template
equal(compileCount, 3);
});
});
});