Skip to content

Commit 111ac3a

Browse files
committed
Allow referring to outerblocks from non-nested components
1 parent b0913d4 commit 111ac3a

File tree

6 files changed

+45
-7
lines changed

6 files changed

+45
-7
lines changed

src/TwigComponent/src/BlockStack.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,6 @@ public function convert(array $blocks, int $targetEmbeddedTemplateIndex): array
3636
// identifies the block definition.
3737
$hostEmbeddedTemplateIndex = $this->findHostEmbeddedTemplateIndex();
3838

39-
if (0 === $hostEmbeddedTemplateIndex) {
40-
// If there is no embedded template index, that means we're in a normal template.
41-
// It wouldn't make sense to make these available as outer blocks,
42-
// since the block is already printed in place.
43-
continue;
44-
}
45-
4639
// Change the name of outer blocks to something unique so blocks of nested components aren't overridden,
4740
// which otherwise might cause a recursion loop when nesting components.
4841
$newName = self::OUTER_BLOCK_PREFIX.$blockName.'_'.mt_rand();
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\UX\TwigComponent\Tests\Fixtures\Component;
13+
14+
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
15+
16+
/**
17+
* @author Bart Vanderstukken <bart.vanderstukken@gmail.com>
18+
*/
19+
#[AsTwigComponent]
20+
final class BasicComponent
21+
{
22+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{% block body %}{% endblock %}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div>
2+
{% block content %}{% endblock %}
3+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{% extends 'base.html.twig' %}
2+
3+
{% block foo %}Hello world!{% endblock %}
4+
5+
{% block body %}
6+
<twig:BasicComponent>
7+
{{ block(outerBlocks.foo) }}
8+
</twig:BasicComponent>
9+
{% endblock %}

src/TwigComponent/tests/Integration/EmbeddedComponentTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ public function testBlockCanBeUsedWithinNestedViaTheOuterBlocks(): void
8080
);
8181
}
8282

83+
/**
84+
* Rule 5 bis: A block inside an extending template can be use inside a component in that template and is NOT rendered in the original location.
85+
*/
86+
public function testBlockCanBeUsedViaTheOuterBlocks(): void
87+
{
88+
$output = self::getContainer()->get(Environment::class)->render('embedded_component_blocks_outer_blocks_extended_template.html.twig');
89+
$this->assertStringContainsStringIgnoringIndentation('<div>Hello world!</div>', $output);
90+
$this->assertStringNotContainsString("Hello world!\n<div", $output);
91+
}
92+
8393
/**
8494
* Rule 8: Defining a block for a component overrides any default content that block has in the component's template.
8595
* This also means that when passing block down that you will lose that default content.

0 commit comments

Comments
 (0)