diff --git a/src/Twig/StimulusTwigExtension.php b/src/Twig/StimulusTwigExtension.php index 9c8b1d37..545d7a00 100644 --- a/src/Twig/StimulusTwigExtension.php +++ b/src/Twig/StimulusTwigExtension.php @@ -59,6 +59,10 @@ public function renderStimulusController(Environment $env, $dataOrControllerName $value = json_encode($value); } + if (is_bool($value)) { + $value = $value ? 'true' : 'false'; + } + $key = twig_escape_filter($env, $this->normalizeKeyName($key), 'html_attr'); $value = twig_escape_filter($env, $value, 'html_attr'); diff --git a/tests/IntegrationTest.php b/tests/IntegrationTest.php index fe8a01a3..ecb4bf1f 100644 --- a/tests/IntegrationTest.php +++ b/tests/IntegrationTest.php @@ -214,7 +214,7 @@ public function provideRenderStimulusController() ], ], 'controllerValues' => [], - 'expected' => 'data-controller="my-controller" data-my-controller-boolean-value="1" data-my-controller-number-value="4" data-my-controller-string-value="str"', + 'expected' => 'data-controller="my-controller" data-my-controller-boolean-value="true" data-my-controller-number-value="4" data-my-controller-string-value="str"', ]; yield 'single-controller-nested-data' => [ @@ -247,7 +247,7 @@ public function provideRenderStimulusController() ], ], 'controllerValues' => [], - 'expected' => 'data-controller="symfony--ux-dropzone--dropzone" data-symfony--ux-dropzone--dropzone-my-key-value="1"', + 'expected' => 'data-controller="symfony--ux-dropzone--dropzone" data-symfony--ux-dropzone--dropzone-my-key-value="true"', ]; yield 'short-single-controller-no-data' => [ @@ -261,6 +261,18 @@ public function provideRenderStimulusController() 'controllerValues' => ['myValue' => 'scalar-value'], 'expected' => 'data-controller="my-controller" data-my-controller-my-value-value="scalar-value"', ]; + + yield 'false-attribute-value-renders-false' => [ + 'dataOrControllerName' => 'false-controller', + 'controllerValues' => ['isEnabled' => false], + 'expected' => 'data-controller="false-controller" data-false-controller-is-enabled-value="false"', + ]; + + yield 'true-attribute-value-renders-true' => [ + 'dataOrControllerName' => 'true-controller', + 'controllerValues' => ['isEnabled' => true], + 'expected' => 'data-controller="true-controller" data-true-controller-is-enabled-value="true"', + ]; } /**