-
-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #936 [TwigComponent] Add variable to refer outer scope when r…
…endering embedded components (sneakyvv) This PR was squashed before being merged into the 2.x branch. Discussion ---------- [TwigComponent] Add variable to refer outer scope when rendering embedded components | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | Tickets | | License | MIT This PR's is an alternative approach to the problem stated in #863. Due to the changes proposed in #920, which deals with passing blocks down to nested embedded components, I realized that just being able to access the host (or parent) component isn't sufficient if blocks can be passed down multiple levels deep (as already mentioned in #863 (comment)). This PR adds a proxy object `Hierarchy` which allows this syntax: `Component.php` ```php final class Component { public function scream(int $screamHowHard = 1): string { return 'sf-ux rules' . str_repeat('!', $screamHowHard); } } ``` ```twig {# component.html.twig #} <twig:Foo :someProp="this.something(1)"> {{ someProp }} {{ outerScope.this.something(3) }} </twig:Foo> ``` ```twig {# Foo.html.twig #} <div>{% block content %}{% endblock %}</div> ``` Resulting in ```twig <div> sf-ux rules! sf-ux rules!!! </div> ``` Adding another layer below Foo, would ask for something like ```twig {# component.html.twig #} <twig:Foo> {{ outerScope.outerScope.this.something(3) }} </twig:Foo> ``` to still point to Foo's function. --- ### Questions There's also a `ComputedPropertiesProxy` object. Should we have a cached version of the `Hierarchy` proxy? --- ### NOTE This PR continues on the changes of #920, because having a hierarchy only makes sense if blocks can be passed down multiple times. So #920 may have to be merged first, but if needed, and the `outerScope.this` syntax is preferred over the syntax of #863, I could push my branch that starts of the main branch instead. Edited: using outerScope variable now instead Hierarchy proxy Commits ------- e618111 [TwigComponent] Add variable to refer outer scope when rendering embedded components
- Loading branch information
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