1.0.0 • Published 9 years ago

htmlattributes-craft v1.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
9 years ago

htmlAttributes

A Craft plugin (Twig filter) to help with the output of HTML attributes.

Usage

Twig template

{% set attrs = {
  class: ['myClass', 'myClass2'],
  style: {
    'background-image': "url('img.png')",
    'color': 'red',
  },
  'data-foo': {
    someKey: 'foo',
    otherKey: 'bar',
    myArray: ['foo', 'bar', 'baz'],
  },
  'data-bar': true,
  'data-baz': null,
  'data-qux': false,
} %}

<div {{ attrs|htmlAttributes }}></div>

htmlAttributes is also aliased to htmlAttrs for convenince.

Output

<div
 class="myClass myClass2"
 style="background-image: url('img.png'); color: red"
 data-foo='{"someKey":"foo","otherKey":"bar","myArray":["foo","bar","baz"]}' data-bar
 data-baz
 ></div>