8000 Merge pull request #698 from drzraf/fix-grandparent-inheritance · twigjs/twig.js@8b722da · GitHub
[go: up one dir, main page]

Skip to content

Commit 8b722da

Browse files
authored
Merge pull request #698 from drzraf/fix-grandparent-inheritance
search for block within all ascendants instead of parent only.
2 parents 3b2f7ec + 8c4d9c2 commit 8b722da

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/twig.core.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,17 +1284,26 @@ module.exports = function (Twig) {
12841284
*
12851285
* @return {Twig.Block|undefined}
12861286
*/
1287-
Twig.Template.prototype.getBlock = function (name, checkOnlyInheritedBlocks) {
1287+
Twig.Template.prototype.getBlock = function (name, checkOnlyInheritedBlocks, checkImports = true) {
12881288
let block;
12891289

12901290
if (checkOnlyInheritedBlocks !== true) {
12911291
block = this.blocks.defined[name];
12921292
}
12931293

1294-
if (block === undefined) {
1294+
if (checkImports && block === undefined) {
12951295
block = this.blocks.imported[name];
12961296
}
12971297

1298+
if (block === undefined && this.parentTemplate !== null) {
1299+
/**
1300+
* Block defined in the parent template when extending.
1301+
* This recursion is useful to inherit from ascendants.
1302+
* But take care of not considering ascendants' {% use %}
1303+
*/
1304+
block = this.parentTemplate.getBlock(name, checkOnlyInheritedBlocks, checkImports = false);
1305+
}
1306+
12981307
return block;
12991308
};
13001309

test/test.inheritance.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const {factory} = require('../twig');
2+
3+
let twig;
4+
5+
describe('Twig.js Blocks ->', function () {
6+
beforeEach(function () {
7+
twig = factory().twig;
8+
});
9+
10+
describe('"extends" tag and inheritance', function () {
11+
it('"extends" applies recursively to grand-parents', function () {
12+
twig({
13+
id: 'grand-parent.twig',
14+
data: '{% block content %}grand-parent.twig{% endblock%}'
15+
});
16+
twig({
17+
id: 'parent.twig',
18+
data: '{% extends "grand-parent.twig" %}'
19+
});
20+
21+
twig({
22+
allowInlineIncludes: true,
23+
data: '{% extends "parent.twig" %}{% block content %}main.twig > {{ parent() }}{% endblock %}'
24+
}).render().should.equal('main.twig > grand-parent.twig');
25+
});
26+
});
27+
});

0 commit comments

Comments
 (0)
0