Skip to content

Commit 8e3d69d

Browse files
committed
bug #171 Do not JSON encode stringable values (jderusse)
This PR was merged into the main branch. Discussion ---------- Do not JSON encode stringable values When using `stimulus_controller` with values generated by twig, the values are converted to JSON. Which prevent the following code working out of the box. ```twig {% set tooltip %} Hello <strong>{{ name }}</strong> {% endset %} <div {{ stimulus_controller('tooltip', {html: true, title: tooltip}) }}> title </div> ``` Because the content of the variable tooltip is an `\Twig\Markup` object. This PR stringifies `values` instead of json_encoding. Commits ------- b7ac5a6 Do not JSON encode stringable values
2 parents a1ce87e + b7ac5a6 commit 8e3d69d

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/Twig/StimulusTwigExtension.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ public function renderStimulusController(Environment $env, $dataOrControllerName
6161
continue;
6262
}
6363

64-
if (!is_scalar($value)) {
64+
if ($value instanceof \Stringable || (\is_object($value) && \is_callable([$value, '__toString']))) {
65+
$value = (string) $value;
66+
} elseif (!\is_scalar($value)) {
6567
$value = json_encode($value);
66-
}
67-
68-
if (\is_bool($value)) {
68+
} elseif (\is_bool($value)) {
6969
$value = $value ? 'true' : 'false';
7070
}
7171

0 commit comments

Comments
 (0)