Skip to content

Commit 84a6803

Browse files
committed
documentation #3797 [StimulusBundle] Document chaining different stimulus_* helpers (Amoifr)
This PR was merged into the 3.x branch. Discussion ---------- [StimulusBundle] Document chaining different stimulus_* helpers | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | Docs? | yes | Issues | Fix #3794 | License | MIT The docs already show chaining the `stimulus_controller`, `stimulus_action` and `stimulus_target` filters with themselves, but not that they can be mixed freely to build all the attributes of an element in one chain, as suggested in #3794. This adds a short section with the example (rendered output verified against the actual helpers) and a unit test covering mixed chaining, which no test exercised before. Commits ------- 5bed5f1 [StimulusBundle] Document chaining different stimulus_* helpers
2 parents c52c1ce + 5bed5f1 commit 84a6803

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

src/StimulusBundle/doc/index.rst

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,34 @@ 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+
>
361+
Hello
362+
</div>
363+
364+
<!-- would render -->
365+
<div data-controller="first-controller fourth-controller"
366+
data-action="first-controller#test fourth-controller#onClick"
367+
data-second-controller-target="anotherTarget"
368+
data-third-controller-target="foo"
369+
>
370+
Hello
371+
</div>
372+
345373
.. _configuration:
346374

347375
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)