diff --git a/extra/html-extra/HtmlExtension.php b/extra/html-extra/HtmlExtension.php
index d8a6c0036d..8eeee5a58f 100644
--- a/extra/html-extra/HtmlExtension.php
+++ b/extra/html-extra/HtmlExtension.php
@@ -14,6 +14,7 @@
use Symfony\Component\Mime\MimeTypes;
use Twig\Error\RuntimeError;
use Twig\Extension\AbstractExtension;
+use Twig\Markup;
use Twig\TwigFilter;
use Twig\TwigFunction;
@@ -92,8 +93,8 @@ public static function htmlClasses(...$args): string
{
$classes = [];
foreach ($args as $i => $arg) {
- if (\is_string($arg)) {
- $classes[] = $arg;
+ if (\is_string($arg) || $arg instanceof Markup) {
+ $classes[] = (string) $arg;
} elseif (\is_array($arg)) {
foreach ($arg as $class => $condition) {
if (!\is_string($class)) {
diff --git a/extra/html-extra/Tests/Fixtures/html_classes.test b/extra/html-extra/Tests/Fixtures/html_classes.test
index 9b26d1564d..7266c1c448 100644
--- a/extra/html-extra/Tests/Fixtures/html_classes.test
+++ b/extra/html-extra/Tests/Fixtures/html_classes.test
@@ -3,15 +3,18 @@
--TEMPLATE--
{{ 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
+{%- set class_b -%}
+b
{%- endset -%}
-{{ html_classes(class_a, { (class_b): true }) }}
-{{ html_classes(class_a, { (class_c): true }) }}
+{{ html_classes(class_a) }}
+{{ html_classes(class_b) }}
+{{ html_classes({ (class_a): true }) }}
+{{ html_classes({ (class_b): true }) }}
--DATA--
return []
--EXPECT--
a b d f 0
-a b
-a c
+a
+b
+a
+b