Skip to content

Add "html_attributes" twig filter for easiely write attributes as objects#3760

Closed
alexander-schranz wants to merge 3 commits into
twigphp:3.xfrom
alexander-schranz:patch-1
Closed

Add "html_attributes" twig filter for easiely write attributes as objects#3760
alexander-schranz wants to merge 3 commits into
twigphp:3.xfrom
alexander-schranz:patch-1

Conversation

@alexander-schranz

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

Copy link
Copy Markdown
Contributor

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

return [
new TwigFilter('data_uri', [$this, 'dataUri']),
new TwigFilter(
'html_attributes',

Copy link
Copy Markdown
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

Comment thread extra/html-extra/HtmlExtension.php Outdated
continue;
}

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

Copy link
Copy Markdown
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

}

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

Copy link
Copy Markdown
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

mpdude commented Dec 7, 2023

Copy link
Copy Markdown
Contributor

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.

@fabpot

fabpot commented Sep 26, 2024

Copy link
Copy Markdown
Contributor

Closing in favor of #3930 (which has the html_attr_merge function in addition to this)

@fabpot fabpot closed this Sep 26, 2024
@alexander-schranz

Copy link
Copy Markdown
Contributor Author

Make sense. Thx @mpdude 👍

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.

4 participants