-
-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TwigComponent] Add variable to refer outer scope when rendering embe…
…dded components
- Loading branch information
1 parent
0950bfa
commit e618111
Showing
9 changed files
with
99 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/TwigComponent/tests/Fixtures/Component/DivComponentWrapper3.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\TwigComponent\Tests\Fixtures\Component; | ||
|
||
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent; | ||
|
||
/** | ||
* @author Bart Vanderstukken <[email protected]> | ||
*/ | ||
#[AsTwigComponent] | ||
final class DivComponentWrapper3 | ||
{ | ||
public string $divComponentWrapperName = 'bar'; | ||
|
||
public function someFunction(): string | ||
{ | ||
return 'calling DivComponentWrapper'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/TwigComponent/tests/Fixtures/templates/components/DivComponentWrapper3.html.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<twig:DivComponent5> | ||
I can access the id from Generic Element as well: {{ id }}. | ||
I can access the properties from DivComponent as well: {{ divComponentName }}. | ||
And of course the properties from DivComponentWrapper: {{ divComponentWrapperName }}. | ||
The less obvious thing is that at this level "this" refers to the component where the content block is used, i.e. the Generic Element. | ||
Therefore, functions through this will be {{ this.someFunction }}. | ||
Calls to outerScope.this will be {{ outerScope.this.someFunction }}. | ||
{{ block(outerBlocks.content) }} | ||
</twig:DivComponent5> |
13 changes: 7 additions & 6 deletions
13
src/TwigComponent/tests/Fixtures/templates/embedded_component_blocks_context.html.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
{% set name = 'Fabien' %} | ||
<twig:DivComponent5> | ||
I can access the id from Generic Element as well: {{ id }}. | ||
I can access the properties from DivComponent as well: {{ divComponentName }}. | ||
The less obvious thing is that at this level "this" refers to the component where the content block is used, i.e. the Generic Element. | ||
Therefore, functions through this will be {{ this.someFunction }}. | ||
</twig:DivComponent5> | ||
<twig:DivComponentWrapper3> | ||
Even I can access the id from Generic Element as well: {{ id }}. | ||
Even I can access the properties from DivComponent as well: {{ divComponentName }}. | ||
Even I can access the properties from DivComponentWrapper as well: {{ divComponentWrapperName }}. | ||
Even I can access the functions of DivComponent via outerScope.this: {{ outerScope.this.someFunction }}. | ||
Since we are nesting two levels deep, calls to outerScope.outerScope.this will be {{ outerScope.outerScope.this.someFunction }}. | ||
</twig:DivComponentWrapper3> |
3 changes: 3 additions & 0 deletions
3
src/TwigComponent/tests/Fixtures/templates/embedded_component_hierarchy_exception.html.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<twig:GenericElement element="div" class="divComponent"> | ||
{{ outerScope.this.foo }} | ||
</twig:GenericElement> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters