File tree Expand file tree Collapse file tree 2 files changed +38
-2
lines changed Expand file tree Collapse file tree 2 files changed +38
-2
lines changed Original file line number Diff line number Diff line change @@ -1284,17 +1284,26 @@ module.exports = function (Twig) {
1284
1284
*
1285
1285
* @return {Twig.Block|undefined }
1286
1286
*/
1287
- Twig . Template . prototype . getBlock = function ( name , checkOnlyInheritedBlocks ) {
1287
+ Twig . Template . prototype . getBlock = function ( name , checkOnlyInheritedBlocks , checkImports = true ) {
1288
1288
let block ;
1289
1289
1290
1290
if ( checkOnlyInheritedBlocks !== true ) {
1291
1291
block = this . blocks . defined [ name ] ;
1292
1292
}
1293
1293
1294
- if ( block === undefined ) {
1294
+ if ( checkImports && block === undefined ) {
1295
1295
block = this . blocks . imported [ name ] ;
1296
1296
}
1297
1297
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
+
1298
1307
return block ;
1299
1308
} ;
1300
1309
Original file line number Diff line number Diff line change
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments