Allow components use dynamically templates

This commit is contained in:
xDeSwa
2026-02-27 23:54:25 +03:00
committed by Hugo Alliaume
parent 56ef59eac4
commit c53c058399
4 changed files with 29 additions and 4 deletions

View File

@@ -3,7 +3,7 @@
## 2.33
- Add `fetch_credentials` option to configure the fetch API credentials mode for cross-origin requests.
This is useful when embedding a Live Component from a different domain that requires cookie-based authentication (e.g., JWT stored in cookies).
This is useful when embedding a Live Component from a different domain that requires cookie-based authentication (e.g., JWT stored in cookies)
Global configuration in `config/packages/live_component.yaml`:
@@ -22,6 +22,8 @@
}
```
- Add support for dynamic template resolution with `AsLiveComponent(template: FromMethod('customFunction'))`
## 2.31
- Add browser events assertions in `InteractsWithLiveComponents`:

View File

@@ -31,7 +31,7 @@
"symfony/property-access": "^5.4.5|^6.0|^7.0|^8.0",
"symfony/property-info": "^5.4|^6.0|^7.0|^8.0",
"symfony/stimulus-bundle": "^2.9",
"symfony/ux-twig-component": "^2.25.1",
"symfony/ux-twig-component": "^2.33.0",
"twig/twig": "^3.10.3"
},
"require-dev": {

View File

@@ -75,6 +75,28 @@ Want some demos? Check out https://ux.symfony.com/live-component#demo
{
// ...
Dynamic Templates
-----------------
.. versionadded:: 2.33
Live components support dynamic template resolution using the ``FromMethod`` attribute, just like standard Twig components.
This is particularly useful for complex live components that need to switch views based on user interaction::
#[AsLiveComponent(template: new FromMethod('getTemplate'))]
class SearchResults
{
#[LiveProp(writable: true)]
public string $layout = 'rows';
public function getTemplate(): string
{
return 'rows' === $this->layout
? 'components/SearchResults/rows.html.twig'
: 'components/SearchResults/grid.html.twig';
}
}
Installation
------------

View File

@@ -13,6 +13,7 @@ namespace Symfony\UX\LiveComponent\Attribute;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
use Symfony\UX\TwigComponent\Attribute\FromMethod;
/**
* An attribute to register a LiveComponent.
@@ -33,7 +34,7 @@ final class AsLiveComponent extends AsTwigComponent
/**
* @param string|null $name The component name (ie: TodoList)
* @param string|null $template The template path of the component (ie: components/TodoList.html.twig).
* @param string|FromMethod|null $template The template path of the component (ie: components/TodoList.html.twig), or a reference to a component's method (ie: FromMethod('getTemplate')
* @param string|null $defaultAction The default action to call when the component is mounted (ie: __invoke)
* @param bool $exposePublicProps Whether to expose every public property as a Twig variable
* @param string $attributesVar The name of the special "attributes" variable in the template
@@ -44,7 +45,7 @@ final class AsLiveComponent extends AsTwigComponent
*/
public function __construct(
?string $name = null,
?string $template = null,
string|FromMethod|null $template = null,
?string $defaultAction = null,
bool $exposePublicProps = true,
string $attributesVar = 'attributes',