PHPackages                             tigr/twig-preprocessor - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. tigr/twig-preprocessor

ActiveLibrary

tigr/twig-preprocessor
======================

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

3.0.0(4y ago)240.5k—10%3MITPHPPHP &gt;=7.0CI failing

Since Jan 9Pushed 4y ago1 watchersCompare

[ Source](https://github.com/TiGR/twig-preprocessor)[ Packagist](https://packagist.org/packages/tigr/twig-preprocessor)[ RSS](/packages/tigr-twig-preprocessor/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (3)Versions (7)Used By (0)

Twig Preprocessor Loader
========================

[](#twig-preprocessor-loader)

[![Build Status](https://camo.githubusercontent.com/ef4548ff48bb8bdfe1f30fe510afbb597d4ed3447dd8a58973ff5a96151e8f61/68747470733a2f2f7472617669732d63692e6f72672f546947522f747769672d70726570726f636573736f722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/TiGR/twig-preprocessor)[![For Twig version](https://camo.githubusercontent.com/579750448c1a18fb45dd9e3330810fb73667a56129471d000355031e5158d6ac/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f747769672d253345253344253230322e302d3838393242462e737667)](http://twig.sensiolabs.org/)[![Minimum PHP Version](https://camo.githubusercontent.com/5c3072425e67297c8ef63d17acd2c86a0d2ef324f19249f2280bd7de902f63a2/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344253230372e302d3838393242462e737667)](https://secure.php.net/)[![Packagist](https://camo.githubusercontent.com/3fcf80d4eba38969c7f020c07911a8ef9128eab144f24e466c7135fc33e0acd7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f546947522f747769672d70726570726f636573736f722e737667)](https://camo.githubusercontent.com/3fcf80d4eba38969c7f020c07911a8ef9128eab144f24e466c7135fc33e0acd7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f546947522f747769672d70726570726f636573736f722e737667)[![GitHub tag (latest SemVer)](https://camo.githubusercontent.com/b3c07ddc8444fd0e5f3f2b14a782988c4f39fec99c24802d3640d7c4f342482e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f7461672f546947522f747769672d70726570726f636573736f722e737667)](https://camo.githubusercontent.com/b3c07ddc8444fd0e5f3f2b14a782988c4f39fec99c24802d3640d7c4f342482e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f7461672f546947522f747769672d70726570726f636573736f722e737667)[![GitHub](https://camo.githubusercontent.com/901b678daee15eb48c497d38251765c5eb4170bc3e269484359ea7bbc2bce22b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f546947522f747769672d70726570726f636573736f722e737667)](https://camo.githubusercontent.com/901b678daee15eb48c497d38251765c5eb4170bc3e269484359ea7bbc2bce22b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f546947522f747769672d70726570726f636573736f722e737667)

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.

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

Installation
------------

[](#installation)

Installation via composer (version for twig 2):

```
composer require tigr/twig-preprocessor

```

If you want to use it with twig 1, use version 1:

```
composer require tigr/twig-preprocessor "~1.0"

```

Usage
-----

[](#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:

```
$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](https://github.com/twigphp/Twig/issues/1005).

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

```
$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
-------

[](#history)

- [Original code in gist](https://gist.github.com/TiGR/5002699).
- [Twig issue about suggested formatting fix](https://github.com/twigphp/Twig/issues/1005).
- [Rejected Twig pull request](https://github.com/twigphp/Twig/pull/1508).

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity32

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~399 days

Total

5

Last Release

1820d ago

Major Versions

1.0.0 → 2.0.12021-05-24

2.0.2 → 3.0.02021-05-24

PHP version history (2 changes)2.0.0PHP &gt;=7.0

1.0.0PHP &gt;=5.2.7

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/281226?v=4)[Igor Tarasov](/maintainers/TiGR)[@TiGR](https://github.com/TiGR)

---

Top Contributors

[![TiGR](https://avatars.githubusercontent.com/u/281226?v=4)](https://github.com/TiGR "TiGR (19 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/tigr-twig-preprocessor/health.svg)

```
[![Health](https://phpackages.com/badges/tigr-twig-preprocessor/health.svg)](https://phpackages.com/packages/tigr-twig-preprocessor)
```

###  Alternatives

[sonata-project/entity-audit-bundle

Audit for Doctrine Entities

644989.8k1](/packages/sonata-project-entity-audit-bundle)[abydahana/aksara

Aksara is a CodeIgniter based CRUD Toolkit you can use to build complex applications become shorter, secure and more reliable just in a few lines of code. Serving both CMS or Framework, produce both HEADLESS (RESTful API) or TRADITIONAL (Browser Based), just by writing single controller. Yet it's reusable, scalable and ready to use!

1101.2k](/packages/abydahana-aksara)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
