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 htmlTagAttributes filter to Twig #4237

Merged
merged 2 commits into from
May 10, 2019
Merged

Add htmlTagAttributes filter to Twig #4237

merged 2 commits into from
May 10, 2019

Conversation

timkelty
Copy link
Contributor

@timkelty timkelty commented May 9, 2019

…because yii\helpers\BaseHtml::renderTagAttributes is awesome!

Back in the day I wrote a Craft 2 plugin for this: https://github.com/timkelty/htmlattributes-craft

Now that it can be added to Craft with a couple lines of code, seems like a big win.

Example usage:

{% set attributes = {
      class: ['one', 'two'],
      disabled: true,
      readonly: false,
      data: {
        baz: 'Escape this "',
        qux: {
          some: ['data', '"quoted"']
        }
      },
      style: {
        'background-color': 'red',
        'font-size': '20px'
      },
} %}

<div {{ attributes|htmlTagAttributes }}></div>

@brandonkelly
Copy link
Member

Couple things:

  • The PR should target the 3.2 branch instead of develop.
  • It should probably be a Twig function rather than filter, since it’s not really modifying the array, as much as generating something completely new using the array for configuration.
  • It should probably just be called attr().

@timkelty timkelty changed the base branch from develop to 3.2 May 10, 2019 10:23
@timkelty
Copy link
Contributor Author

All set, @brandonkelly!

  • The PR should target the 3.2 branch instead of develop.
  • It should probably be a Twig function rather than filter, since it’s not really modifying the array, as much as generating something completely new using the array for configuration.
  • It should probably just be called attr().

@brandonkelly brandonkelly merged commit 8f69e68 into craftcms:3.2 May 10, 2019
@brandonkelly
Copy link
Member

Looks great, thanks!

brandonkelly added a commit that referenced this pull request May 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants