8000 bug #2269 Bugfix: When rendering blocks of other templates, don't pas… · twigphp/Twig@f59b33a · GitHub
[go: up one dir, main page]

Skip to content

Commit f59b33a

Browse files
committed
bug #2269 Bugfix: When rendering blocks of other templates, don't pass the local blocks array (derrabus, fabpot)
This PR was merged into the 1.x branch. Discussion ---------- Bugfix: When rendering blocks of other templates, don't pass the local blocks array This PR fixes symfony/symfony#20556. When referring to a block of another template from within a template that also has a block with the same name, Twig renders the wrong block. Commits ------- 8c090a7 added tests for block() when using a template argument a5526f3 fixed block() is defined call when using a template argument c04d077 Bugfix: When rendering blocks of other templates, don't pass the local blocks array.
2 parents a1eb0f5 + 8c090a7 commit f59b33a

File tree

4 files changed

+74
-28
lines changed

4 files changed

+74
-28
lines changed

CHANGELOG

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
* 1.28.1 (2016-XX-XX)
22

3-
* n/a
3+
* fixed block() function when used with a template argument
44

55
* 1.28.0 (2016-11-17)
66

lib/Twig/Node/Expression/BlockReference.php

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,46 +39,53 @@ public function __construct(Twig_NodeInterface $name, $template = null, $lineno,
3939
public function compile(Twig_Compiler $compiler)
4040
{
4141
if ($this->getAttribute('is_defined_test')) {
42-
$compiler
43-
->raw('$this->hasBlock(')
44-
->subcompile($this->getNode('name'))
45-
->raw(', $context, $blocks)')
46-
;
42+
$this->compileTemplateCall($compiler, 'hasBlock');
4743
} else {
4844
if ($this->getAttribute('output')) {
4945
$compiler->addDebugInfo($this);
5046

5147
$this
52-
->compileTemplateCall($compiler)
53-
->raw('->displayBlock(')
54-
->subcompile($this->getNode('name'))
55-
->raw(", \$context, \$blocks);\n")
56-
;
48+
->compileTemplateCall($compiler, 'displayBlock')
49+
->raw(";\n");
5750
} else {
58-
$this
59-
->compileTemplateCall($compiler)
60-
->raw('->renderBlock(')
61-
->subcompile($this->getNode('name'))
62-
->raw(', $context, $blocks)')
63-
;
51+
$this->compileTemplateCall($compiler, 'renderBlock');
6452
}
6553
}
6654
}
6755

68-
private function compileTemplateCall(Twig_Compiler $compiler)
56+
private function compileTemplateCall(Twig_Compiler $compiler, $method)
57+
{
58+
if (!$this->hasNode('template')) {
59+
$compiler->write('$this');
60+
} else {
61+
$compiler
62+
->write('$this->loadTemplate(')
63+
->subcompile($this->getNode('template'))
64+
->raw(', ')
65+
->repr($this->getTemplateName())
66+
->raw(', ')
67+
->repr($this->getTemplateLine())
68+
->raw(')')
69+
;
70+
}
71+
72+
$compiler->raw(sprintf('->%s', $method));
73+
$this->compileBlockArguments($compiler);
74+
75+
return $compiler;
76+
}
77+
78+
private function compileBlockArguments(Twig_Compiler $compiler)
6979
{
80+
$compiler
81+
->raw('(')
82+
->subcompile($this->getNode('name'))
83+
->raw(', $context');
84+
7085
if (!$this->hasNode('template')) {
71-
return $compiler->write('$this');
86+
$compiler->raw(', $blocks');
7287
}
7388

74-
return $compiler
75-
->write('$this->loadTemplate(')
76-
->subcompile($this->getNode('template'))
77-
->raw(', ')
78-
->repr($this->getTemplateName())
79-
->raw(', ')
80-
->repr($this->getTemplateLine())
81-
->raw(')')
82-
;
89+
return $compiler->raw(')');
8390
}
8491
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
"block" function with a template argument
3+
--TEMPLATE--
4+
{{ block('foo', 'included.twig') }}
5+
{{ block('foo', included_loaded) }}
6+
{{ block('foo', included_loaded_internal) }}
7+
{% set output = block('foo', 'included.twig') %}
8+
{{ output }}
9+
{% block foo %}NOT FOO{% endblock %}
10+
--TEMPLATE(included.twig)--
11+
{% block foo %}FOO{% endblock %}
12+
--DATA--
13+
return array(
14+
'included_loaded' => $twig->load('included.twig'),
15+
'included_loaded_internal' => $twig->loadTemplate('included.twig'),
16+
)
17+
--EXPECT--
18+
FOO
19+
FOO
20+
FOO
21+
FOO
22+
NOT FOO
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
"defined" support for blocks with a template argument
3+
--TEMPLATE--
4+
{{ block('foo', 'included.twig') is defined ? 'ok' : 'ko' }}
5+
{{ block('foo', included_loaded) is defined ? 'ok' : 'ko' }}
6+
{{ block('foo', included_loaded_internal) is defined ? 'ok' : 'ko' }}
7+
--TEMPLATE(included.twig)--
8+
{% block foo %}FOO{% endblock %}
9+
--DATA--
10+
return array(
11+
'included_loaded' => $twig->load('included.twig'),
12+
'included_loaded_internal' => $twig->loadTemplate('included.twig'),
13+
)
14+
--EXPECT--
15+
ok
16+
ok
17+
ok

0 commit comments

Comments
 (0)
0