html-extra icon indicating copy to clipboard operation
html-extra copied to clipboard

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

Open alexander-schranz opened this issue 3 years ago • 4 comments

Example usage:

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

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

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

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

TODO

  • [ ] Add Test Case
  • [ ] Escape Value

alexander-schranz avatar Oct 09 '22 15:10 alexander-schranz

Shouldnt you edit https://github.com/twigphp/Twig/tree/3.x/extra/html-extra instead? I think this is a subtree split of twig extra components

regarding the PR, i like the idea :)

94noni avatar Oct 13 '22 19:10 94noni

@94noni haha thanks 😄

alexander-schranz avatar Oct 14 '22 08:10 alexander-schranz

https://github.com/alexander-schranz/html-extra/blob/87248a0e840255405398b17dd37eabe9a850b1e6/HtmlExtension.php#L104

continue; is out of the scope. $value is the same.

gassan avatar Jan 09 '23 18:01 gassan

Incidentally I made a very similar suggestion at https://github.com/twigphp/Twig/pull/3907 with a PoC PR at https://github.com/twigphp/Twig/pull/3930.

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

mpdude avatar Dec 07 '23 09:12 mpdude