The Craft 3 version is here!
Ever have a client put 4 MB PNG files in their Redactor fields, failing to apply any of your meticulously crafted image transforms? Writers consistently using the <strong>
tag for headers? Have you ever needed to implement lazy loading of images embedded in WYSIWYG content, responsive images or wanted to remove pesky inline styles without breaking a sweat? This is for you.
Retcon is a small Craft CMS plugin offering a series of easy-to-use Twig filters for manipulating HTML content.
- Download & unzip
- Move the /retconhtml folder to craft/plugins
- Install from Control Panel
Please see the Wiki page for documentation, featureset overview and code examples.
Retcon uses DOMDocument to rewrite HTML. It includes a series of different methods, exposed as Twig filters:
{{entry.body | retconTransform('someImageTransform')}}
All methods, however, can also be called as template methods:
{{craft.retconHtml.transform(entry.body, 'someImageTransform')}}
...or through the Retcon service, if you ever want to use it in your PHP:
<?php echo craft()->retconHtml->transform($entry->body, 'someImageTransform'); ?>
If you prefer, Retcon also includes a "catch-all" filter, taking the filter name as its first argument:
{{entry.body | retcon('transform', 'someImageTransform')}}
{{craft.retconHtml.retcon(entry.body, 'transform', 'someImageTransform')}}
<?php echo craft()->retconHtml->retcon($entry->body, 'transform', 'someImageTransform'); ?>
And finally, you'll also be able to apply several operations in one go (for a theoretical performance gain). Each index in the operation array will be either a String value (filter name) if there are no arguments, or an array of arguments (where the filter name should be the first index).
{{entry.body | retcon([
['transform', 'someImageTransform'],
'lazy',
['attr', '.foo', {'class' : 'bar'}]
])}}
{{craft.retconHtml.retcon(entry.body, [
['transform', 'someImageTransform'],
'lazy',
['attr', '.foo', {'class' : 'bar'}]
])}}
<?php
echo craft()->retconHtml->retcon($entry->body, array(
array('transform', 'someImageTransform'),
'lazy',
array('attr', '.foo', array('class' => 'bar')),
));
?>
transform Apply a named or inline image transform to all images. If installed, Retcon uses Imager to apply the transform.
srcset Apply an array of named or inline image transform to all images, for simple srcset support. If installed, Retcon uses Imager to apply the transforms.
lazy Replaces the src attribute of image tags with a transparent, 1x1 px base64 encoded SVG, placing the original image's src in a data-attribute and retaining the original image's aspect ratio
autoAlt Adds filename as alternative text for images missing alt tags
attr
Adds, replaces or removes a set of attributes and attribute values – e.g. class
. Can be used to remove inline styles.
renameAttr Renames existing attributes for matching selectors, retaining the attribute values.
wrap Wraps stuff in other stuff
unwrap Removes parent node for matching elements
remove Removes all elements matching the given selector(s)
only Removes everything but the elements matching the given selector(s)
change Changes tag type for all elements matching the given selector(s). Can also remove the tag(s) completely, retaining inner content
inject Inject strings or HTML
replace Replace stuff with ```preg_replace``
By default, Retcon will store any transformed images to a subfolder below the original image (identical to Craft's behaviour when applying transforms to an AssetFileModel). If you want to store transforms somewhere else, you can set a different base path here. Make sure that the folder is writable!
Self explanatory.
HTML output will be encoded to UTF-8 by default, but you can set the encoding to be anything you want.
If installed, Retcon will use the Imager plugin by André Elvan for all image transforms. You can turn it off, though.
Retcon is provided free of charge. The author is not responsible for data loss or any other problems resulting from the use of this plugin. Please see the Wiki page for documentation and examples. and report any bugs, feature requests or other issues here. As Retcon is a hobby project, no promises are made regarding response time, feature implementations or bug amendments.
- [Fixed] The retconOnly filter no longer returns matching elements in reversed order
- [Fixed] Fixes an issue where absolute transform URLs were messed up.
- [Improved] The retconChange filter is now able to remove tag(s) completely for matching selectors, retaining content
- [Added] Adds retconSrcset filter for simple responsive images
- [Added] Adds retconRenameAttr filter for renaming attribute names
- [Added] Adds setting to turn off Imager support
- [Improved] The rectonLazy filter base64 string now retains the aspect ratio for the original image
- [Fixed] Fixed an issue where a missing $options parameter to RetconHtmlDocument would throw a warning in PHP7
- [Added] Its now possible to add attributes without values
- [Improved] Reasons now longer throws PHP exceptions for empty strings
- [Added] Adds release feed, documentation URL and other Craft 2.5 features
- [Improved] Now uses André Elvan's Imager plugin for image transforms (where available)
- Fixed an issue where certain filters would only apply to the first matched selector
- Added
replace
filter
- Fixed typo in settings template
- Fixed issue with undefined constant CRAFT_SITE_URL
- Fixed issue with hostname parsing for image URLs (could cause images to not be recognized as local assets)
- Fixed issue with uppercase file extensions not being allowed (could cause image transforms to be rejected)
- Initial public release