Skip to content
M. Mikkel Rummelhoff edited this page Mar 5, 2018 · 1 revision

Attr is a powerful filter which enables you to add, add to, replace or remove attributes for one or more selectors.

Usage/examples

Set class for all p tags (will remove any existing classes)

{{ entry.body | retconAttr( 'p', { 'class' : 'someClass' } ) }}

Add a class for all p tags (will not remove any existing classes)

{{ entry.body | retconAttr( 'p', { 'class' : 'someClass' }, false ) }}

Add a class to several selectors at once

{{ entry.body | retconAttr( [ '.foo', '.bar' ], { 'class' : 'someClass' } ) }}

Remove inline styles for all elements

{{ entry.body | retconAttr( '*', { 'style' : false } ) }}

Add multiple attributes

{{ entry.body | retconAttr( '.foo', { 'class' : 'bar', 'data-baz' : 'qux' } ) }}

Add an attribute without a value

{{ entry.body | retconAttr( 'script', { 'async' : true } ) }}

Remove attributes

{{ entry.body | retconAttr( 'img', { 'width' : false, 'height' : false } ) }}

Parameters

@selectors Mixed
String value or Array of string values
Examples: "p" ".foo" "#bar" "div.baz"

@attributes Array
Associative Array of attributes and attribute values.
Tip: Set attribute value to false or null to remove the attribute.

@overwrite Boolean
Optional, default true
If true, any existing attribute values will be replaced. If false, attribute values will be added.