Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Stringable #4467

Open
wants to merge 1 commit into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions extra/html-extra/HtmlExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
}

Expand Down
5 changes: 5 additions & 0 deletions extra/html-extra/Tests/Fixtures/html_classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Loading