Skip to content

Commit 629d834

Browse files
committed
possible way to remove need for |raw
1 parent 171374c commit 629d834

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/TwigComponent/src/Twig/ComponentExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ final class ComponentExtension extends AbstractExtension
2424
public function getFunctions(): array
2525
{
2626
return [
27-
new TwigFunction('component', [ComponentRuntime::class, 'render'], ['is_safe' => ['all']]),
27+
new TwigFunction('component', [ComponentRuntime::class, 'render'], ['is_safe' => ['all'], 'needs_environment' => true]),
2828
];
2929
}
3030
}

src/TwigComponent/src/Twig/ComponentRuntime.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111

1212
namespace Symfony\UX\TwigComponent\Twig;
1313

14+
use Symfony\UX\TwigComponent\AttributeBag;
1415
use Symfony\UX\TwigComponent\ComponentFactory;
1516
use Symfony\UX\TwigComponent\ComponentRenderer;
17+
use Twig\Environment;
18+
use Twig\Extension\EscaperExtension;
1619

1720
/**
1821
* @author Kevin Bond <[email protected]>
@@ -23,15 +26,22 @@ final class ComponentRuntime
2326
{
2427
private ComponentFactory $componentFactory;
2528
private ComponentRenderer $componentRenderer;
29+
private bool $safeClassesRegistered = false;
2630

2731
public function __construct(ComponentFactory $componentFactory, ComponentRenderer $componentRenderer)
2832
{
2933
$this->componentFactory = $componentFactory;
3034
$this->componentRenderer = $componentRenderer;
3135
}
3236

33-
public function render(string $name, array $props = []): string
37+
public function render(Environment $twig, string $name, array $props = []): string
3438
{
39+
if (!$this->safeClassesRegistered) {
40+
$twig->getExtension(EscaperExtension::class)->addSafeClass(AttributeBag::class, ['html']);
41+
42+
$this->safeClassesRegistered = true;
43+
}
44+
3545
return $this->componentRenderer->render(
3646
$this->componentFactory->create($name, $props),
3747
$this->componentFactory->configFor($name)['template']

0 commit comments

Comments
 (0)