Skip to content

Add Twig function and filter for Stimulus Outlets integration #206

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

Closed
wants to merge 5 commits into from
Closed
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
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ class ScriptNonceSubscriber implements EventSubscriberInterface
### stimulus_controller

This bundle also ships with a special `stimulus_controller()` Twig function
that can be used to render [Stimulus Controllers & Values](https://stimulus.hotwired.dev/reference/values)
that can be used to render [Stimulus Controllers & Values](https://stimulus.hotwired.dev/reference/values), [Outlets](https://stimulus.hotwired.dev/reference/outlets)
and [CSS Classes](https://stimulus.hotwired.dev/reference/css-classes).
See [stimulus-bridge](https://github.com/symfony/stimulus-bridge) for more details.

Expand Down Expand Up @@ -260,6 +260,17 @@ If you have multiple controllers on the same element, you can chain them as ther
</div>
```

If you need to attach an [outlet](https://stimulus.hotwired.dev/reference/outlets) to the controller, you can call the `addOutlet()` method.

For example:

```twig
<div {{ stimulus_controller('chart').addOutlet('outlet-controller', '.css-selector') }}>Hello</div>

<!-- would render -->
<div data-controller="chart" data-chart-outlet-controller-outlet=".css-selector">Hello</div>
```

You can also retrieve the generated attributes as an array, which can be helpful e.g. for forms:

```twig
Expand Down
21 changes: 19 additions & 2 deletions src/Dto/StimulusControllersDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@

namespace Symfony\WebpackEncoreBundle\Dto;

use Twig\Markup;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed


final class StimulusControllersDto extends AbstractStimulusDto
{
private $controllers = [];
private $values = [];
private $outlets = [];
private $classes = [];

public function addController(string $controllerName, array $controllerValues = [], array $controllerClasses = []): void
Expand All @@ -36,10 +39,21 @@ public function addController(string $controllerName, array $controllerValues =
foreach ($controllerClasses as $key => $class) {
$key = $this->escapeAsHtmlAttr($this->normalizeKeyName($key));

$this->values['data-'.$controllerName.'-'.$key.'-class'] = $class;
$this->classes['data-'.$controllerName.'-'.$key.'-class'] = $class;
}
}

public function addOutlet(string $outletName, string $selector)
{
if (1 < \count($this->controllers)) {
throw new \LengthException('You cannot call addOutlet() method when passing more than one controller identifier to stimulus_controller() function');
}

$this->outlets['data-'.$this->controllers[0].'-'.$outletName.'-outlet'] = $selector;

return new Markup($this, 'UTF-8');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the Markup class suddenly needed in this situation?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm probably doing something wrong but if I don't use this class, the result becomes : data-controller=""controller"" data-controller-foo-outlet="".foo""

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird. It make me think that, below in __toString(), when we call $this->escapeAsHtmlAttr($value), that is taking .foo and turning it into ".foo"... but that seems very odd...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's also clean when I use the raw filter like this : {{ stimulus_controller('foo').addOutlet('bar', '.bar')|raw }}, but it's certainly not what we need.

}

public function __toString(): string
{
if (0 === \count($this->controllers)) {
Expand All @@ -53,7 +67,10 @@ public function __toString(): string
}, array_keys($this->values), $this->values)).' '.
implode(' ', array_map(function (string $attribute, string $value): string {
return $attribute.'="'.$this->escapeAsHtmlAttr($value).'"';
}, array_keys($this->classes), $this->classes))
}, array_keys($this->classes), $this->classes)).' '.
implode(' ', array_map(function (string $attribute, string $value): string {
return $attribute.'="'.$this->escapeAsHtmlAttr($value).'"';
}, array_keys($this->outlets), $this->outlets))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test for this - in StimulusControllersDtoTest?

);
}

Expand Down
16 changes: 16 additions & 0 deletions tests/Dto/StimulusControllersDtoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,20 @@ public function testToArrayNoEscapingAttributeValues(): void
$attributesArray
);
}

public function testAddOutlet(): void
{
$this->stimulusControllersDto->addController('foo', ['bar' => '"'], ['baz' => '"']);
$this->stimulusControllersDto->addOutlet('outlet-name', '"');
$attributesArray = $this->stimulusControllersDto->toArray();
self::assertSame(
[
'data-controller' => 'foo',
'data-foo-bar-value' => '"',
'data-foo-baz-class' => '"',
'data-foo-outlet-name-outlet' => '"',
],
$attributesArray
);
}
}