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 "html_attributes" twig filter for easiely write attributes as objects #3760

Draft
wants to merge 3 commits into
base: 3.x
Choose a base branch
from

Conversation

alexander-schranz
Copy link
Contributor

@alexander-schranz alexander-schranz commented Oct 14, 2022

Example usage:

<!-- button.html.twig -->
{# required #}
{%- set text = text -%}

{# optional #}
{%- set id = id|default(null) -%}
{%- set skin = skin|default('primary') -%}
{%- set type = type|default('button') -%}
{%- set disabled = disabled|default(false) -%}
{%- set href = href|default(null) -%}

{% set attributes = {
    'id': id,
    'class': html_classes(
        'c-button',
        {
            'c-button--primary': skin == 'primary',
            'c-button--secondary': skin == 'secondary',
            'c-button--borderless': skin == 'borderless',
        },
    ),
    'href': href,
    'type': not href ? type : null,
    'disabled': disabled,
} %}

{% set tag = href ? 'a' : 'button' %}

<{{ tag }} {{ attributes|html_attributes }}>
    {{- text -}}
</{{ tag }}>

TODO

  • Escape Value
  • Add Test Case

@@ -28,6 +28,14 @@ public function getFilters(): array
{
return [
new TwigFilter('data_uri', [$this, 'dataUri']),
new TwigFilter(
'html_attributes',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks more like a function than a filter to me

continue;
}

$htmlAttributes[] = $key . '="' . twig_escape_filter($environment, $value, 'html_attr') . '"';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keys must be escaped too

@@ -79,6 +87,35 @@ public function dataUri(string $data, string $mime = null, array $parameters = [

return $repr;
}

/**
* @param array{string, string|bool|int|float|null} $attributes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong type. array{string, string|bool|int|float|null} means an array with 2 elements, with index 0 being a string and index 1 being string|bool|int|float|null. Instead, you should use array<string, string|bool|int|float|null>

@mpdude
Copy link
Contributor

mpdude commented Dec 7, 2023

Incidentally I made a very similar suggestion at #3907 with a PoC PR at #3930.

Would be great if you could 👀 and maybe we can join efforts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants