Skip to content

Commit

Permalink
fix: locals in inherited layout
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonihc committed Dec 2, 2021
1 parent b74c2d0 commit 4f33548
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ function handleExtendsNodes(tree, options, messages) {
locals = JSON.parse(extendsNode.attrs.locals);
} catch {}
}
options.expressions.locals = merge(options.expressions.locals, locals);
options.plugins.push(expressions(options.expressions));

var options_expressions = merge(options.expressions, {locals});
var plugins = [...options.plugins, expressions(options_expressions)];

const layoutPath = path.resolve(options.root, extendsNode.attrs.src);
const layoutHtml = fs.readFileSync(layoutPath, options.encoding);
const layoutTree = handleExtendsNodes(applyPluginsToTree(parseToPostHtml(layoutHtml), options.plugins), options, messages);
const layoutTree = handleExtendsNodes(applyPluginsToTree(parseToPostHtml(layoutHtml), plugins), options, messages);

extendsNode.tag = false;
extendsNode.content = mergeExtendsAndLayout(layoutTree, extendsNode, options.strict, options.slotTagName, options.fillTagName);
Expand Down
36 changes: 36 additions & 0 deletions test/extend.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,42 @@ describe('Extend', () => {
});
});

it('should accept locals in inherited layout', () => {
mfs.writeFileSync('./parent.html', `
<div class="parent">
<span>{{ parent_var }}</span>
<block name="content"></block>
</div>
`);

mfs.writeFileSync('./child.html', `
<div class="child">
<span>{{ child_var }}</span>
<block name="child_content"></block>
</div>
`);

return init(`
<extends src="parent.html" locals='{"parent_var":"parent var sample"}'>
<block name="content">
<extends src="child.html" locals='{"child_var":"child var sample"}'>
<block name="child_content">child content example</block>
</extends>
</block>
</extends>
`, {strict: false}).then(html => {
expect(html).toBe(cleanHtml(`
<div class="parent">
<span>parent var sample</span>
<div class="child">
<span>child var sample</span>
child content example
</div>
</div>
`));
});
});

it('should extend inherited layout', () => {
mfs.writeFileSync('./base.html', `
<html>
Expand Down

0 comments on commit 4f33548

Please sign in to comment.