Skip to content

Commit 460db75

Browse files
authored
Merge pull request #45 from nelsonihc/master
fix: locals in inherited layout
2 parents b74c2d0 + 2a4ea5c commit 460db75

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

src/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,13 @@ function handleExtendsNodes(tree, options, messages) {
4747
locals = JSON.parse(extendsNode.attrs.locals);
4848
} catch {}
4949
}
50+
5051
options.expressions.locals = merge(options.expressions.locals, locals);
51-
options.plugins.push(expressions(options.expressions));
52+
const plugins = [...options.plugins, expressions(options.expressions)];
5253

5354
const layoutPath = path.resolve(options.root, extendsNode.attrs.src);
5455
const layoutHtml = fs.readFileSync(layoutPath, options.encoding);
55-
const layoutTree = handleExtendsNodes(applyPluginsToTree(parseToPostHtml(layoutHtml), options.plugins), options, messages);
56+
const layoutTree = handleExtendsNodes(applyPluginsToTree(parseToPostHtml(layoutHtml), plugins), options, messages);
5657

5758
extendsNode.tag = false;
5859
extendsNode.content = mergeExtendsAndLayout(layoutTree, extendsNode, options.strict, options.slotTagName, options.fillTagName);

test/extend.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,42 @@ describe('Extend', () => {
154154
});
155155
});
156156

157+
it('should accept locals in inherited layout', () => {
158+
mfs.writeFileSync('./parent.html', `
159+
<div class="parent">
160+
<span>{{ parent_var }}</span>
161+
<block name="content"></block>
162+
</div>
163+
`);
164+
165+
mfs.writeFileSync('./child.html', `
166+
<div class="child">
167+
<span>{{ child_var }}</span>
168+
<block name="child_content"></block>
169+
</div>
170+
`);
171+
172+
return init(`
173+
<extends src="parent.html" locals='{"parent_var":"parent var sample"}'>
174+
<block name="content">
175+
<extends src="child.html" locals='{"child_var":"child var sample"}'>
176+
<block name="child_content">child content example</block>
177+
</extends>
178+
</block>
179+
</extends>
180+
`, {strict: false}).then(html => {
181+
expect(html).toBe(cleanHtml(`
182+
<div class="parent">
183+
<span>parent var sample</span>
184+
<div class="child">
185+
<span>child var sample</span>
186+
child content example
187+
</div>
188+
</div>
189+
`));
190+
});
191+
});
192+
157193
it('should extend inherited layout', () => {
158194
mfs.writeFileSync('./base.html', `
159195
<html>

0 commit comments

Comments
 (0)