PHPackages                             maceda/twig-composer - 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. [Templating &amp; Views](/categories/templating)
4. /
5. maceda/twig-composer

ActiveLibrary[Templating &amp; Views](/categories/templating)

maceda/twig-composer
====================

Callback library for Twig

v0.5(9y ago)00Public DomainPHP

Since Oct 31Pushed 7y ago1 watchersCompare

[ Source](https://github.com/AlvaroMaceda/twig-composer)[ Packagist](https://packagist.org/packages/maceda/twig-composer)[ Docs](https://github.com/AlvaroMaceda/twig-composer)[ RSS](/packages/maceda-twig-composer/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (3)Versions (3)Used By (0)

Twig Composer
=============

[](#twig-composer)

A callback function library for Twig: you will be able to execute code when templates are rendered.

Useful for calling code like controllers, etc. before the template is rendered. This can be done in some frameworks as Symfony or Laravel, but this library works directly with Twig.

For example, if you have a `menu.twig` template which can be included in a layout, you can do:

```
    TwigComposer::getNotifier()->on('menu.twig', [$menuService,generateDinamicMenu]);

```

And then use the menu generated in `menu.twig` template. You won't need to include a call to the service in each controller which uses the layout.

Getting Started
---------------

[](#getting-started)

If you are using composer tool, you can start using TwigComposer requiring the library:

```
require maceda/twig-composer

```

If not, please follow installation instructions.

To be notified of template renders, you need to indicate TwigComposer as the base class for templates when creating Twig\_Environment:

```
$this->twig = new \Twig_Environment(
$loader,
array(
     ...
     'base_template_class' => 'TwigComposer\TwigComposer',
     ...
));

```

To receive callbacks you must register them for each template you want to be notified:

```
TwigComposer::getNotifier()->on('Template_I_Want_To_Watch', $callback);

```

`$callback` will be called each time `'Template_I_Want_To_Watch'` is rendered.

You can also inherit your own base class from TwigComposer if you need to use your own base class for Twig (do not forget to call parent methods when corresponding)

### Installation

[](#installation)

If you do not use composer, you must install and require the following dependencies:

- [christopherobin/EventEmitter](http://daringfireball.net/projects/markdown/)
- [Twig](http://twig.sensiolabs.org/)

You must also include `Relayer.php` and `TwigComposer.php` in your project to use this library.

Running the tests
-----------------

[](#running-the-tests)

This library uses PHPUnit for test. To run the test, execute this command:

```
/usr/bin/php ./vendor/phpunit/phpunit/phpunit ./tests

```

If you want to generate code coverage information, just add the option `--coverage-html DIRECTORY`

Contributing
------------

[](#contributing)

Feel free to contribute to this code. Please send an email to the authors and / or a pull request to the project's repository:

Authors
-------

[](#authors)

- **[Alvaro Maceda](http://alvaromaceda.es)** - *Initial work*

License
-------

[](#license)

This project is Public Domain.

Acknowledgments
---------------

[](#acknowledgments)

- Thanks to [Christophe Robin](https://github.com/christopherobin) for his port of [event emmiter](http://daringfireball.net/projects/markdown/) to PHP
- Inspired by View::composer feature of Laravel

###  Health Score

22

—

LowBetter than 21% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity0

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 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

Unknown

Total

1

Last Release

3528d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/372743?v=4)[alvaro](/maintainers/alvaromaceda)[@AlvaroMaceda](https://github.com/AlvaroMaceda)

---

Top Contributors

[![AlvaroMaceda](https://avatars.githubusercontent.com/u/372743?v=4)](https://github.com/AlvaroMaceda "AlvaroMaceda (24 commits)")

---

Tags

twigcallbacks

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/maceda-twig-composer/health.svg)

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

###  Alternatives

[timber/timber

Create WordPress themes with beautiful OOP code and the Twig Template Engine

5.7k3.6M127](/packages/timber-timber)[twig/intl-extra

A Twig extension for Intl

36567.2M320](/packages/twig-intl-extra)[symfony/ux-twig-component

Twig components for Symfony

22017.2M313](/packages/symfony-ux-twig-component)[symfony/ux-live-component

Live components for Symfony

1636.5M115](/packages/symfony-ux-live-component)[twig/cssinliner-extra

A Twig extension to allow inlining CSS

22919.7M81](/packages/twig-cssinliner-extra)[twig/inky-extra

A Twig extension for the inky email templating engine

16613.2M70](/packages/twig-inky-extra)

PHPackages © 2026

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