Skip to content

Commit cb86fed

Browse files
AmoifrKocal
authored andcommitted
[StimulusBundle] Document chaining different stimulus_* helpers
1 parent 4d84b54 commit cb86fed

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

src/StimulusBundle/doc/index.rst

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,32 @@ You can also retrieve the generated attributes as an array, which can be helpful
342342
343343
{{ form_row(form.password, { attr: stimulus_target('hello-controller', 'myTarget').toArray() }) }}
344344
345+
Chaining Different Helpers
346+
~~~~~~~~~~~~~~~~~~~~~~~~~~
347+
348+
The ``stimulus_controller``, ``stimulus_action`` and ``stimulus_target``
349+
filters can be mixed freely to render all the attributes of an element at
350+
once, whichever function the chain started with:
351+
352+
.. code-block:: html+twig
353+
354+
<div {{ stimulus_controller('first-controller')
355+
|stimulus_target('second-controller', 'anotherTarget')
356+
|stimulus_target('third-controller', 'foo')
357+
|stimulus_action('first-controller', 'test')
358+
|stimulus_controller('fourth-controller')
359+
|stimulus_action('fourth-controller', 'onClick') }}>
360+
Hello
361+
</div>
362+
363+
<!-- would render -->
364+
<div data-controller="first-controller fourth-controller"
365+
data-action="first-controller#test fourth-controller#onClick"
366+
data-second-controller-target="anotherTarget"
367+
data-third-controller-target="foo">
368+
Hello
369+
</div>
370+
345371
.. _configuration:
346372

347373
Configuration

src/StimulusBundle/tests/Twig/StimulusTwigExtensionTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,4 +250,20 @@ public function testAppendStimulusTarget()
250250
(string) $extension->appendStimulusTarget($dto, '@symfony/ux-dropzone/dropzone', 'anotherTarget fooTarget')
251251
);
252252
}
253+
254+
public function testAppendDifferentStimulusHelpers()
255+
{
256+
$extension = new StimulusTwigExtension(new StimulusHelper($this->twig));
257+
$dto = $extension->renderStimulusController('first-controller');
258+
$dto = $extension->appendStimulusTarget($dto, 'second-controller', 'anotherTarget');
259+
$dto = $extension->appendStimulusTarget($dto, 'third-controller', 'foo');
260+
$dto = $extension->appendStimulusAction($dto, 'first-controller', 'test');
261+
$dto = $extension->appendStimulusController($dto, 'fourth-controller');
262+
$dto = $extension->appendStimulusAction($dto, 'fourth-controller', 'onClick');
263+
264+
$this->assertSame(
265+
'data-controller="first-controller fourth-controller" data-action="first-controller#test fourth-controller#onClick" data-second-controller-target="anotherTarget" data-third-controller-target="foo"',
266+
(string) $dto
267+
);
268+
}
253269
}

0 commit comments

Comments
 (0)