|
| 1 | +# WebfactoryHtml5TagRewriterBundle |
| 2 | + |
| 3 | +This bundle provides Symfony integration for the [webfactory/html5-tagrewriter](https://github.com/webfactory/html5-tagrewriter) package. |
| 4 | + |
| 5 | +Core feature is the Twig filter `rewriteTags`, which allows sections of HTML5 in Twig templates to be processed by RewriteHandlers. RewriteHandlers can be configured in the container through tags or using an autoconfiguration attribute. |
| 6 | + |
| 7 | +See the [webfactory/html5-tagrewriter documentation](https://github.com/webfactory/html5-tagrewriter) for details on what a RewriteHandler is and how to implement one. Processing is based on the PHP 8.4 DOM HTML5 parser (`Dom\HTMLDocument`). |
| 8 | + |
| 9 | +## Usage in Twig |
| 10 | + |
| 11 | +Use the `rewriteTags` filter to process HTML through a TagRewriter: |
| 12 | + |
| 13 | +```twig |
| 14 | +{# With the apply tag #} |
| 15 | +{% apply rewriteTags %} |
| 16 | + <a href="https://example.com">Link</a> |
| 17 | +{% endfilter %} |
| 18 | +
|
| 19 | +{# ... or the pipe notation #} |
| 20 | +{{ content|rewriteTags }} |
| 21 | +
|
| 22 | +{# Using a named TagRewriter #} |
| 23 | +{{ content|rewriteTags('special') }} |
| 24 | +``` |
| 25 | + |
| 26 | +## Registering RewriteHandlers |
| 27 | + |
| 28 | +### Using the `#[AsRewriteHandler]` Attribute (Recommended) |
| 29 | + |
| 30 | +The easiest way to register a RewriteHandler is using the `#[AsRewriteHandler]` attribute. With autowiring and autoconfiguration enabled, the handler will be automatically registered: |
| 31 | + |
| 32 | +```php |
| 33 | +use Webfactory\Html5TagRewriter\Handler\BaseRewriteHandler; |
| 34 | +use Webfactory\Html5TagRewriterBundle\Attribute\AsRewriteHandler; |
| 35 | +use Dom\Element; |
| 36 | + |
| 37 | +#[AsRewriteHandler] |
| 38 | +class MyRewriteHandler extends BaseRewriteHandler |
| 39 | +{ |
| 40 | + public function appliesTo(): string |
| 41 | + { |
| 42 | + return '//html:a'; |
| 43 | + } |
| 44 | + |
| 45 | + public function match(Element $element): void |
| 46 | + { |
| 47 | + $element->setAttribute('target', '_blank'); |
| 48 | + } |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +### Attribute Options |
| 53 | + |
| 54 | +The `#[AsRewriteHandler]` attribute accepts two optional parameters: |
| 55 | + |
| 56 | +- `priority` (int, default: `0`): Higher values are processed first |
| 57 | +- `rewriter` (string or array, default: `'default'`): The name(s) of the TagRewriter instance(s) to register with |
| 58 | + |
| 59 | +```php |
| 60 | +// Register with high priority on the default rewriter |
| 61 | +#[AsRewriteHandler(priority: 100)] |
| 62 | +class HighPriorityHandler extends BaseRewriteHandler { /* ... */ } |
| 63 | + |
| 64 | +// Register on a named rewriter |
| 65 | +#[AsRewriteHandler(rewriter: 'special')] |
| 66 | +class SpecialHandler extends BaseRewriteHandler { /* ... */ } |
| 67 | + |
| 68 | +// Register on multiple rewriters |
| 69 | +#[AsRewriteHandler(rewriter: ['default', 'special'])] |
| 70 | +class SharedHandler extends BaseRewriteHandler { /* ... */ } |
| 71 | + |
| 72 | +// Combine priority and named rewriter |
| 73 | +#[AsRewriteHandler(priority: 50, rewriter: 'special')] |
| 74 | +class PrioritizedSpecialHandler extends BaseRewriteHandler { /* ... */ } |
| 75 | +``` |
| 76 | + |
| 77 | +### Using Service Tags |
| 78 | + |
| 79 | +If you need more control or cannot use autoconfiguration, you can manually tag your services with `webfactory.html5_tag_rewriter.rewrite_handler`: |
| 80 | + |
| 81 | +```php |
| 82 | +// config/services.php |
| 83 | +use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; |
| 84 | + |
| 85 | +return static function (ContainerConfigurator $container): void { |
| 86 | + $services = $container->services(); |
| 87 | + |
| 88 | + $services->set(App\Handler\MyRewriteHandler::class) |
| 89 | + // ... arguments or other configuration here |
| 90 | + ->tag('webfactory.html5_tag_rewriter.rewrite_handler'); |
| 91 | + |
| 92 | + $services->set(App\Handler\SpecialHandler::class) |
| 93 | + // ... arguments or other configuration here |
| 94 | + ->tag('webfactory.html5_tag_rewriter.rewrite_handler', [ |
| 95 | + 'rewriter' => 'special', |
| 96 | + 'priority' => 10, |
| 97 | + ]); |
| 98 | +}; |
| 99 | +``` |
| 100 | + |
| 101 | +## Credits, Copyright and License |
| 102 | + |
| 103 | +This bundle is based on internal work that we have been using at webfactory GmbH, Bonn, since 2019. |
| 104 | +However, that (old) bundle implementation was written for the legacy PHP DOM extension, leading to |
| 105 | +several quirks in HTML processing and requiring the use of [Polyglot HTML 5](https://www.w3.org/TR/html-polyglot/). |
| 106 | + |
| 107 | +Thus, we decided to overhaul the bundle for PHP 8.4 HTML5 DOM support and re-release it as open source. |
| 108 | + |
| 109 | +- <https://www.webfactory.de> |
| 110 | + |
| 111 | +Copyright 2026 webfactory GmbH, Bonn. Code released under [the MIT license](LICENSE). |
0 commit comments