2017 © Pedro Peláez
 

library twig-preprocessor

This very simple loader adds a way to pre parse twig files before loading them into twig parser

image

tigr/twig-preprocessor

This very simple loader adds a way to pre parse twig files before loading them into twig parser

  • Monday, January 9, 2017
  • by TiGR
  • Repository
  • 1 Watchers
  • 0 Stars
  • 3,150 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 4 Versions
  • 29 % Grown

The README.md

Twig Preprocessor Loader

Build Status For Twig version Minimum PHP Version Packagist GitHub tag (latest SemVer) GitHub, (*1)

This Twig Preprocessor loader allows you to do custom manipulations with twig templates before passing them to twig parser. This allows you, for instance, to do some substitutions, or format templates to make them look better., (*2)

This branch (master) contains code for Twig 2.x., (*3)

Installation

Installation via composer (version for twig 2):, (*4)

composer require tigr/twig-preprocessor

If you want to use it with twig 1, use version 1:, (*5)

composer require tigr/twig-preprocessor "~1.0"

Usage

In general usage, you pass real template loader to Twig Preprocessor Loader and also a callback that would be called to mingle with twig template code. So, general usage is this:, (*6)

$realLoader = Twig_Loader_Filesystem('/path/to/templates');
$loader = Twig_Loader_Preprocessor(
    $realLoader, 
    function($twigSource) {
        // do something with $twigSource

        return $twigSource;
    }
);

$twig = new Twig_Environment($loader);

The main reason why this code was written was to make Twig output a bit more pretty-formatted code. You can read more about this in this Twig issue., (*7)

In short, the idea is to remove all spaces/tabs before any (well, most) twig control structures. Here is how you can achieve this:, (*8)

$loader = new Twig_Loader_Preprocessor(
    $realLoader,
    function ($template) {
        static $regExp;
        // the RE isn't perfect, it won't match structures having curly braces within, 
        // but it's okay for me.
        if (!isset($regExp)) {
            $regExp = str_replace(
                ['_',      '{',  '}'],
                ['[\t ]*', '\{', '\}'],
                '/^_({([#%])[^}]*[^-](?2)}|{%_block_\w*_%}{%_endblock_%})$/m'
            );
        }

        return preg_replace($regExp, '$1', $template);
    }
);

History

The Versions

09/01 2017

dev-twig1

dev-twig1

This very simple loader adds a way to pre parse twig files before loading them into twig parser

  Sources   Download

MIT

The Requires

 

The Development Requires

09/01 2017

dev-master

9999999-dev

This very simple loader adds a way to pre parse twig files before loading them into twig parser

  Sources   Download

MIT

The Requires

 

The Development Requires

09/01 2017

1.0.0

1.0.0.0

This very simple loader adds a way to pre parse twig files before loading them into twig parser

  Sources   Download

MIT

The Requires

 

The Development Requires

09/01 2017

2.0.0

2.0.0.0

This very simple loader adds a way to pre parse twig files before loading them into twig parser

  Sources   Download

MIT

The Requires

 

The Development Requires