Skip to content

Commit 41fa3ea

Browse files
committed
[StimulusBundle] Document chaining different stimulus_* helpers
1 parent 4d84b54 commit 41fa3ea

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

src/StimulusBundle/doc/index.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,30 @@ 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+
Hello
359+
</div>
360+
361+
<!-- would render -->
362+
<div data-controller="first-controller"
363+
data-action="first-controller#test"
364+
data-second-controller-target="anotherTarget"
365+
data-third-controller-target="foo">
366+
Hello
367+
</div>
368+
345369
.. _configuration:
346370

347371
Configuration

src/StimulusBundle/tests/Twig/StimulusTwigExtensionTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,4 +250,18 @@ 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+
262+
$this->assertSame(
263+
'data-controller="first-controller" data-action="first-controller#test" data-second-controller-target="anotherTarget" data-third-controller-target="foo"',
264+
(string) $dto
265+
);
266+
}
253267
}

0 commit comments

Comments
 (0)