Add "html_attributes" twig filter for easiely write attributes as objects
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
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 haha thanks 😄
https://github.com/alexander-schranz/html-extra/blob/87248a0e840255405398b17dd37eabe9a850b1e6/HtmlExtension.php#L104
continue; is out of the scope. $value is the same.
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.