PHP code example of maba / twig-template-modification-bundle

1. Go to this page and download the library: Download maba/twig-template-modification-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/ */

    

maba / twig-template-modification-bundle example snippets


new Maba\Bundle\WebpackBundle\MabaTwigTemplateModificationBundle(),

use Maba\Bundle\TwigTemplateModificationBundle\Service\TwigNodeReplacerInterface;
use Maba\Bundle\TwigTemplateModificationBundle\Entity\TemplateContext;
use Twig_Node as Node;

class MyNodeReplacer implements TwigNodeReplacerInterface
{
        /**
         * @param Node $node
         * @param TemplateContext $context
         *
         * @return null|string string if this node should be replaced with given twig code
         */
        public function replace(Node $node, TemplateContext $context)
        {
            if ($node instanceof NameExpression && $node->getAttribute('name') === 'my_var') {
                return '123';
            }
            return null;
        }
}

$factory = $container->get('maba_twig_template_modification.factory.files_replacer');
$replacer = $factory->createFilesReplacer(new MyNodeReplacer());

// both arguments (closures) are optional
$replacer->replace(function($filePath, $contents, $notices) {
    // log or write to output before replacing file in $filePath with $contents
}, function (array $notices) use ($output) {
    // log or write to output notices
});