Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Twig] Add attributes() twig function #1414

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/TwigComponent/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## Unreleased

- Add `attributes()` twig function.

## 2.13.0

- [BC BREAK] Add component metadata to `PreMountEvent` and `PostMountEvent`
Expand Down
22 changes: 18 additions & 4 deletions src/TwigComponent/src/ComponentRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,28 @@ public function finishEmbeddedComponentRender(): void
$this->dispatcher->dispatch($event);
}

private function preRender(MountedComponent $mounted, array $context = []): PreRenderEvent
public function createAttributes(array $attributes = []): ComponentAttributes
{
if (!$this->safeClassesRegistered) {
$this->twig->getExtension(EscaperExtension::class)->addSafeClass(ComponentAttributes::class, ['html']);
$this->registerSafeClasses();

return new ComponentAttributes($attributes);
}

$this->safeClassesRegistered = true;
private function registerSafeClasses(): void
{
if ($this->safeClassesRegistered) {
return;
}

$this->twig->getExtension(EscaperExtension::class)->addSafeClass(ComponentAttributes::class, ['html']);

$this->safeClassesRegistered = true;
}

private function preRender(MountedComponent $mounted, array $context = []): PreRenderEvent
{
$this->registerSafeClasses();

$component = $mounted->getComponent();
$metadata = $this->factory->metadataFor($mounted->getName());
// expose public properties and properties marked with ExposeInTemplate attribute
Expand Down
7 changes: 7 additions & 0 deletions src/TwigComponent/src/Twig/ComponentExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Psr\Container\ContainerInterface;
use Symfony\Contracts\Service\ServiceSubscriberInterface;
use Symfony\UX\TwigComponent\ComponentAttributes;
use Symfony\UX\TwigComponent\ComponentRenderer;
use Symfony\UX\TwigComponent\Event\PreRenderEvent;
use Twig\Error\RuntimeError;
Expand Down Expand Up @@ -41,6 +42,7 @@ public function getFunctions(): array
{
return [
new TwigFunction('component', [$this, 'render'], ['is_safe' => ['all']]),
new TwigFunction('attributes', [$this, 'attributes']),
];
}

Expand All @@ -61,6 +63,11 @@ public function render(string $name, array $props = []): string
}
}

public function attributes(array $attributes = []): ComponentAttributes
{
return $this->container->get(ComponentRenderer::class)->createAttributes($attributes);
}

public function extensionPreCreateForRender(string $name, array $props): ?string
{
try {
Expand Down
10 changes: 10 additions & 0 deletions src/TwigComponent/tests/Integration/ComponentExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,16 @@ public function testComponentPropsWithTrailingComma(): void
$this->assertStringContainsString('Hello FOO, 123, and 456', $output);
}

public function testAttributesFunction(): void
{
$output = self::getContainer()->get(Environment::class)
->createTemplate('<div{{ attributes({class: "foo", "data-controller": "bar"}) }}/>')
->render()
;

$this->assertSame('<div class="foo" data-controller="bar"/>', $output);
}

private function renderComponent(string $name, array $data = []): string
{
return self::getContainer()->get(Environment::class)->render('render_component.html.twig', [
Expand Down