diff --git a/extra/html-extra/HtmlExtension.php b/extra/html-extra/HtmlExtension.php index d8a6c0036d1..41ac0c48e06 100644 --- a/extra/html-extra/HtmlExtension.php +++ b/extra/html-extra/HtmlExtension.php @@ -92,20 +92,20 @@ public static function htmlClasses(...$args): string { $classes = []; foreach ($args as $i => $arg) { - if (\is_string($arg)) { - $classes[] = $arg; + if (\is_string($arg) || $arg instanceof \Stringable) { + $classes[] = (string) $arg; } elseif (\is_array($arg)) { foreach ($arg as $class => $condition) { - if (!\is_string($class)) { - throw new RuntimeError(\sprintf('The "html_classes" function argument %d (key %d) should be a string, got "%s".', $i, $class, get_debug_type($class))); + if (!\is_string($class) && !$class instanceof \Stringable) { + throw new RuntimeError(\sprintf('The "html_classes" function argument %d (key %d) should be a string or an instance of Stringable, got "%s".', $i, $class, get_debug_type($class))); } if (!$condition) { continue; } - $classes[] = $class; + $classes[] = (string) $class; } } else { - throw new RuntimeError(\sprintf('The "html_classes" function argument %d should be either a string or an array, got "%s".', $i, get_debug_type($arg))); + throw new RuntimeError(\sprintf('The "html_classes" function argument %d should be either a string, an instance of Stringable or an array, got "%s".', $i, get_debug_type($arg))); } } diff --git a/extra/html-extra/Tests/Fixtures/html_classes.test b/extra/html-extra/Tests/Fixtures/html_classes.test index 65ecaba6ada..d05d55f14ab 100644 --- a/extra/html-extra/Tests/Fixtures/html_classes.test +++ b/extra/html-extra/Tests/Fixtures/html_classes.test @@ -4,9 +4,14 @@ {{ html_classes('a', {'b': true, 'c': false}, 'd', false ? 'e', true ? 'f', '0') }} {% set class_a = 'a' %} {% set class_b = 'b' %} +{%- set class_c -%} +c +{%- endset -%} {{ html_classes(class_a, {(class_b): true})}} +{{ html_classes(class_a, {(class_c): true})}} --DATA-- return [] --EXPECT-- a b d f 0 a b +a c diff --git a/extra/html-extra/Tests/Fixtures/html_classes_with_unsupported_arg.test b/extra/html-extra/Tests/Fixtures/html_classes_with_unsupported_arg.test index 21ca373f818..631dc1b99e1 100644 --- a/extra/html-extra/Tests/Fixtures/html_classes_with_unsupported_arg.test +++ b/extra/html-extra/Tests/Fixtures/html_classes_with_unsupported_arg.test @@ -5,4 +5,4 @@ --DATA-- return [] --EXCEPTION-- -Twig\Error\RuntimeError: The "html_classes" function argument 0 should be either a string or an array, got "bool" in "index.twig" at line 2. +Twig\Error\RuntimeError: The "html_classes" function argument 0 should be either a string, an instance of Stringable or an array, got "bool" in "index.twig" at line 2. diff --git a/extra/html-extra/Tests/Fixtures/html_classes_with_unsupported_key.test b/extra/html-extra/Tests/Fixtures/html_classes_with_unsupported_key.test index 708cb255bbc..c3a84f99799 100644 --- a/extra/html-extra/Tests/Fixtures/html_classes_with_unsupported_key.test +++ b/extra/html-extra/Tests/Fixtures/html_classes_with_unsupported_key.test @@ -5,4 +5,4 @@ --DATA-- return [] --EXCEPTION-- -Twig\Error\RuntimeError: The "html_classes" function argument 0 (key 0) should be a string, got "int" in "index.twig" at line 2. +Twig\Error\RuntimeError: The "html_classes" function argument 0 (key 0) should be a string or an instance of Stringable, got "int" in "index.twig" at line 2.