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 #4

Closed
wants to merge 3 commits into from

Conversation

alexander-schranz
Copy link

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

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

continue;
}

$htmlAttributes[] .= $key . '="' . $value . '"';
Copy link
Author

Choose a reason for hiding this comment

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

TODO escpae $value with html_attr:

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

requires 'needs_environment' => true,

@94noni
Copy link

94noni commented Oct 13, 2022

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 :)

@alexander-schranz
Copy link
Author

@94noni haha thanks 😄

@gassan
Copy link

gassan commented Jan 9, 2023

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

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

@mpdude
Copy link

mpdude commented Dec 7, 2023

Incidentally I made a very similar suggestion at twigphp/Twig#3907 with a PoC PR at twigphp/Twig#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.

5 participants