-
-
Notifications
You must be signed in to change notification settings - Fork 84
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
Changes from all commits
7d2a8e2
c01ebfa
4ba8250
744e0e6
7a210c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,10 +11,13 @@ | |
|
||
namespace Symfony\WebpackEncoreBundle\Dto; | ||
|
||
use Twig\Markup; | ||
|
||
final class StimulusControllersDto extends AbstractStimulusDto | ||
{ | ||
private $controllers = []; | ||
private $values = []; | ||
private $outlets = []; | ||
private $classes = []; | ||
|
||
public function addController(string $controllerName, array $controllerValues = [], array $controllerClasses = []): void | ||
|
@@ -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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 : There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Weird. It make me think that, below in There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) { | ||
|
@@ -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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a test for this - in |
||
); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not needed