PHP code example of webfactory / html5-tagrewriter-bundle

1. Go to this page and download the library: Download webfactory/html5-tagrewriter-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

webfactory / html5-tagrewriter-bundle example snippets


use Webfactory\Html5TagRewriter\Handler\BaseRewriteHandler;
use Webfactory\Html5TagRewriterBundle\Attribute\AsRewriteHandler;
use Dom\Element;

#[AsRewriteHandler]
class MyRewriteHandler extends BaseRewriteHandler
{
    public function appliesTo(): string
    {
        return '//html:a';
    }

    public function match(Element $element): void
    {
        $element->setAttribute('target', '_blank');
    }
}

// Register with high priority on the default rewriter
#[AsRewriteHandler(priority: 100)]
class HighPriorityHandler extends BaseRewriteHandler { /* ... */ }

// Register on a named rewriter
#[AsRewriteHandler(rewriter: 'special')]
class SpecialHandler extends BaseRewriteHandler { /* ... */ }

// Register on multiple rewriters
#[AsRewriteHandler(rewriter: ['default', 'special'])]
class SharedHandler extends BaseRewriteHandler { /* ... */ }

// Combine priority and named rewriter
#[AsRewriteHandler(priority: 50, rewriter: 'special')]
class PrioritizedSpecialHandler extends BaseRewriteHandler { /* ... */ }

// config/services.php
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $container): void {
    $services = $container->services();

    $services->set(App\Handler\MyRewriteHandler::class)
        // ... arguments or other configuration here
        ->tag('webfactory.html5_tag_rewriter.rewrite_handler');

    $services->set(App\Handler\SpecialHandler::class)
        // ... arguments or other configuration here
        ->tag('webfactory.html5_tag_rewriter.rewrite_handler', [
            'rewriter' => 'special',
            'priority' => 10,
        ]);
};