-
-
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 #913 [TwigComponent] [LiveComponent] Add support for embedded…
… live components (sneakyvv) This PR was squashed before being merged into the 2.x branch. Discussion ---------- [TwigComponent] [LiveComponent] Add support for embedded live components | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | Tickets | | License | MIT ## Context Using embedded components was introduced in #317, but support for Live embedded components was not added. The issue is that on a re-render of a Live component you lose the blocks defined within an embedded component. This PR solves that issue. ## Example To explain the solution, take this example: ```twig {# someTemplate.html.twig #} {% component Foo %} {% block content %} Override content {% endblock %} {% endcomponent %} ``` ```twig {# Foo.html.twig #} <div {{ attributes }}> {% block content %} Default content {% endblock %} </div> ``` Of course, Foo is a Live component. This obviously also works with the new Twig syntax. ## Background 1. Each `{% component %}` tag is compiled by `ComponentNode`. It adds an embedded Template to the Template class compiled for `someTemplate.html.twig`. This is a second class inside the same php file, with a suffix in the form of `___%d`. That number at the end is normally random, and is called the embedded template index. 2. `ComponentNode` would generate Template code which fetches the `embeddedContext` from the `ComponentRenderer` and passed that to the `loadTemplate('Foo.html.twig', $index)->display()` 3. When a component is re-rendered (via an action callback) it uses the template of the component `(Foo.html.twig`), which does not have the original block content, because that's part of the host Template (`someTemplate.html.twig`). ## Solution We only need to use the embedded Template instead of the component Template to re-render a component. To make this happen, we need to: 1. Use a deterministic index for an embedded template during compilation. 2. Store info on the rendered component's HTML (via the attributes) about the host template and the embedded template's index. 3. Load the embedded Template during re-render using the info passed along with the other attributes/props. ## Remaining 1. I use `loadTemplate` now in the `ComponentRender`, which is marked as internal in the Twig package. Can we ignore that within this package (as both are "Symfony")? 2. The `PreRenderEvent::EMBEDDED` constant and the `isEmbedded` function were introduced to block live embedded components. Should this PR remove those as well? ### Tasks - [ ] Remove `isEmbedded`? - [ ] Remove `PreRenderEvent::EMBEDDED`? - [ ] Add CHANGELOG Commits ------- d9dd3fc [TwigComponent] [LiveComponent] Add support for embedded live components
- Loading branch information
Showing
23 changed files
with
421 additions
and
83 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?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\LiveComponent\Twig; | ||
|
||
use Symfony\Component\Cache\Adapter\NullAdapter; | ||
use Symfony\Component\Cache\Adapter\PhpArrayAdapter; | ||
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; | ||
|
||
/** | ||
* @author Bart Vanderstukken <[email protected]> | ||
* | ||
* @internal | ||
*/ | ||
final class TemplateCacheWarmer implements CacheWarmerInterface | ||
{ | ||
public function __construct(private \IteratorAggregate $templateIterator, private readonly string $cacheFilename) | ||
{ | ||
} | ||
|
||
public function warmUp(string $cacheDir): void | ||
{ | ||
$map = []; | ||
foreach ($this->templateIterator as $item) { | ||
$map[bin2hex(random_bytes(16))] = $item; | ||
} | ||
|
||
(new PhpArrayAdapter($cacheDir.'/'.$this->cacheFilename, new NullAdapter()))->warmUp(['map' => $map]); | ||
} | ||
|
||
public function isOptional(): bool | ||
{ | ||
return false; | ||
} | ||
} |
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,45 @@ | ||
<?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\LiveComponent\Twig; | ||
|
||
use Symfony\Component\Cache\Adapter\NullAdapter; | ||
use Symfony\Component\Cache\Adapter\PhpArrayAdapter; | ||
|
||
/** | ||
* @author Bart Vanderstukken <[email protected]> | ||
* | ||
* @internal | ||
*/ | ||
final class TemplateMap | ||
{ | ||
private readonly array $map; | ||
|
||
public function __construct(string $cacheFile) | ||
{ | ||
$this->map = (new PhpArrayAdapter($cacheFile, new NullAdapter()))->getItem('map')->get(); | ||
} | ||
|
||
public function resolve(string $obscuredName) | ||
{ | ||
return $this->map[$obscuredName] ?? throw new \RuntimeException(sprintf('Cannot find a template matching "%s". Cache may be corrupt.', $obscuredName)); | ||
} | ||
|
||
public function obscuredName(string $templateName): string | ||
{ | ||
$obscuredName = array_search($templateName, $this->map, true); | ||
if (false === $obscuredName) { | ||
throw new \RuntimeException(sprintf('Cannot find a match for template "%s". Cache may be corrupt.', $templateName)); | ||
} | ||
|
||
return $obscuredName; | ||
} | ||
} |
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
4 changes: 3 additions & 1 deletion
4
src/LiveComponent/tests/Fixtures/templates/components/component2.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,4 +1,6 @@ | ||
<div{{ attributes }}> | ||
<div{{ attributes.defaults({class: 'component2'}) }} > | ||
{% block content %} | ||
Count: {{ this.count }} | ||
PreReRenderCalled: {{ this.preReRenderCalled ? 'Yes' : 'No' }} | ||
{% endblock %} | ||
</div> |
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
6 changes: 6 additions & 0 deletions
6
src/LiveComponent/tests/Fixtures/templates/render_embedded_with_blocks.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,6 @@ | ||
{% component component2 %} | ||
{% block content %} | ||
{{ parent() }} | ||
Embedded content with access to context, like count={{ this.count }} | ||
{% endblock %} | ||
{% endcomponent %} |
2 changes: 2 additions & 0 deletions
2
src/LiveComponent/tests/Fixtures/templates/render_multiple_embedded_with_blocks.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,2 @@ | ||
<div id="component1">{% component component2 %}{% block content %}Overridden content from component 1{% endblock %}{% endcomponent %}</div><div id="component2">{% component component2 %}{% block content %}Overridden content from component 2 on same line - count: {{ this.count }}{% endblock %}{% endcomponent %}</div> | ||
<div id="component3">Not overriding{% component component2 %}{% endcomponent %}</div> |
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
Oops, something went wrong.