From 0aa40dd099104aaccb6820af646f849d73d44ca3 Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Fri, 14 Oct 2022 10:26:34 +0200 Subject: [PATCH 1/3] Add "html_attributes" twig filter for easiely write attributes as objects --- extra/html-extra/HtmlExtension.php | 36 ++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/extra/html-extra/HtmlExtension.php b/extra/html-extra/HtmlExtension.php index ed740b47187..cd0ff64a650 100644 --- a/extra/html-extra/HtmlExtension.php +++ b/extra/html-extra/HtmlExtension.php @@ -28,6 +28,14 @@ public function getFilters(): array { return [ new TwigFilter('data_uri', [$this, 'dataUri']), + new TwigFilter( + 'html_attributes', + [$this, 'htmlAttributes'], + [ + 'is_safe' => ['html'], + 'needs_environment' => true, + ] + ), ]; } @@ -79,6 +87,34 @@ public function dataUri(string $data, string $mime = null, array $parameters = [ return $repr; } + + /** + * @param array{string, string|bool|null} $attributes + */ + public function htmlAttributes(Environment $environment, array $attributes): string + { + /** @var string[] $htmlAttributes */ + $htmlAttributes = []; + foreach ($attributes as $key => $value) { + if (\is_bool($value)) { + // false should never be outputted e.g. disabled, readonly + // this matches also the behaviour here: https://github.com/symfony/symfony/blob/v5.4.14/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig#L465-L476 + if ($value) { + $htmlAttributes[] = $key; + } + + continue; + } + + if (null === $value) { + continue; + } + + $htmlAttributes[] = $key . '="' . twig_escape_filter($environment, $value, 'html_attr') . '"'; + } + + return implode(' ', $htmlAttributes); + } } } From c3f03ba44ccbd3d2ce4f38c109ae38197d639f7c Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Fri, 14 Oct 2022 10:31:34 +0200 Subject: [PATCH 2/3] Update HtmlExtension.php --- extra/html-extra/HtmlExtension.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/extra/html-extra/HtmlExtension.php b/extra/html-extra/HtmlExtension.php index cd0ff64a650..13dfae7637e 100644 --- a/extra/html-extra/HtmlExtension.php +++ b/extra/html-extra/HtmlExtension.php @@ -89,7 +89,7 @@ public function dataUri(string $data, string $mime = null, array $parameters = [ } /** - * @param array{string, string|bool|null} $attributes + * @param array{string, string|bool|int|float|null} $attributes */ public function htmlAttributes(Environment $environment, array $attributes): string { @@ -106,6 +106,7 @@ public function htmlAttributes(Environment $environment, array $attributes): str continue; } + // null represent no value and should not be outputted for a better DX if (null === $value) { continue; } From b2c19b472f73b4c90b08fd009ea227452405c8b9 Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Sat, 17 Dec 2022 20:15:34 +0100 Subject: [PATCH 3/3] Fix problem with escape urls as html_attr instead of html --- extra/html-extra/HtmlExtension.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extra/html-extra/HtmlExtension.php b/extra/html-extra/HtmlExtension.php index 13dfae7637e..e0cf3342425 100644 --- a/extra/html-extra/HtmlExtension.php +++ b/extra/html-extra/HtmlExtension.php @@ -111,7 +111,7 @@ public function htmlAttributes(Environment $environment, array $attributes): str continue; } - $htmlAttributes[] = $key . '="' . twig_escape_filter($environment, $value, 'html_attr') . '"'; + $htmlAttributes[] = $key . '="' . twig_escape_filter($environment, $value) . '"'; } return implode(' ', $htmlAttributes);