Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

DataAttributesExtension.php
Go to the documentation of this file.
1 <?php
2
7 declare(strict_types=1);
8
9 namespace eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension;
10
11 use Twig\Extension\AbstractExtension;
12 use Twig\TwigFilter;
13
17 class DataAttributesExtension extends AbstractExtension
18 {
24 public function getFilters(): array
25 {
26 return [
27 new TwigFilter(
28 'ez_data_attributes_serialize',
29 [$this, 'serializeDataAttributes'],
30 ['is_safe' => ['html']]
31 ),
32 ];
33 }
34
43 public function serializeDataAttributes(array $dataAttributes): string
44 {
45 $result = '';
46 foreach ($dataAttributes as $attributeName => $attributeValue) {
47 if (!is_string($attributeValue)) {
48 $attributeValue = json_encode($attributeValue);
49 }
50
51 $result .= sprintf('data-%s="%s" ', $attributeName, htmlspecialchars($attributeValue));
52 }
53
54 return rtrim($result);
55 }
56 }
eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\DataAttributesExtension
Twig common extension for general use helpers.
Definition: DataAttributesExtension.php:18
eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\DataAttributesExtension\serializeDataAttributes
serializeDataAttributes(array $dataAttributes)
Processes an associative list of data attributes and returns them as HTML attributes list in the form...
Definition: DataAttributesExtension.php:43
eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\DataAttributesExtension\getFilters
getFilters()
Returns a list of functions to add to the existing list.
Definition: DataAttributesExtension.php:24
eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension
Definition: ContentExtension.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.