Skip to content

Commit

Permalink
Add support for Twig\Markup
Browse files Browse the repository at this point in the history
Co-authored-by: Fabien Potencier <fabien@potencier.org>
  • Loading branch information
nlemoine and fabpot committed Dec 29, 2024
1 parent 38688f7 commit 8158dab
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
5 changes: 3 additions & 2 deletions extra/html-extra/HtmlExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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

0 comments on commit 8158dab

Please sign in to comment.