2017 © Pedro PelĆ”ez
 

library twig-template-modification-bundle

Bundle to Mass-Modify Twig Templates

image

maba/twig-template-modification-bundle

Bundle to Mass-Modify Twig Templates

  • Saturday, June 11, 2016
  • by mariusbalcytis
  • Repository
  • 1 Watchers
  • 0 Stars
  • 114 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 3 % Grown

The README.md

Bundle to Mass-Modify Twig Templates

What does this bundle do?

This bundle does not do anything by itself - it helps you to modify your existing twig templates by replacing parsed nodes with some other twig code., (*1)

It could be used to mass-edit many templates at once., (*2)

For usage example see maba/webpack-migration-bundle., (*3)

Installation

composer require maba/twig-template-modification-bundle

Inside AppKernel:, (*4)

new Maba\Bundle\WebpackBundle\MabaTwigTemplateModificationBundle(),

Usage

Make service which implements TwigNodeReplacerInterface., (*5)

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;
        }
}

replace method will be called on each and every node in every twig template., (*6)

If string is returned (not null), node will be replaced with given string content., (*7)

TemplateContext holds template name. You can also add notices to it (for example unable to replace some node) and hold attributes (and reuse them later - same context is passed for each node in same template)., (*8)

Initiate template rewrites with this code:, (*9)

$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
});

You could also create replacer with dependency injection using factory service:, (*10)

<service id="acme.files_replacer"
         class="Maba\Bundle\TwigTemplateModificationBundle\Service\FilesReplacer">
     <factory service="maba_twig_template_modification.factory.files_replacer" method="createFilesReplacer"/>
    <argument type="collection">
        <argument type="service" id="acme.my_node_replacer"/>
    </argument>
</service>

Important! Code replaces content in templates in your bundles and app directory - be sure to run it only when using version control system with no changes uncommitted., (*11)

Running tests

Travis status, (*12)

composer install
vendor/bin/phpunit

The Versions

11/06 2016

dev-master

9999999-dev

Bundle to Mass-Modify Twig Templates

  Sources   Download

MIT

The Requires

 

The Development Requires

by Marius Balčytis

twig templates find-replace mass-edit mass-replace mass-modify modification

11/06 2016

1.0.1

1.0.1.0

Bundle to Mass-Modify Twig Templates

  Sources   Download

MIT

The Requires

 

The Development Requires

by Marius Balčytis

twig templates find-replace mass-edit mass-replace mass-modify modification

11/06 2016

1.0.0

1.0.0.0

Bundle to Mass-Modify Twig Templates

  Sources   Download

MIT

The Requires

 

The Development Requires

by Marius Balčytis

twig templates find-replace mass-edit mass-replace mass-modify modification