PHPackages                             b2r/twig - 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. b2r/twig

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

b2r/twig
========

Twig descendants

v0.2.1(9y ago)0431MITPHPPHP &gt;=7.0

Since Mar 13Pushed 9y ago1 watchersCompare

[ Source](https://github.com/b2r/php-twig)[ Packagist](https://packagist.org/packages/b2r/twig)[ Docs](https://github.com/b2r/php-twig)[ RSS](/packages/b2r-twig/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependencies (2)Versions (4)Used By (1)

b2rPHP: Twig
============

[](#b2rphp-twig)

Twig composition

[![Build Status](https://camo.githubusercontent.com/cb0494d71c67d312b6aa62dde30f1c8d7882361abe4f6f78aa2caf9e6b01263a/68747470733a2f2f7472617669732d63692e6f72672f6232722f7068702d747769672e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/b2r/php-twig)

- [CHANGELOG](CHANGELOG.md)
- [Packagist](https://packagist.org/packages/b2r/twig)

Features
--------

[](#features)

- Fluent interface
- Smart loader
- Context container

Simple Usage
------------

[](#simple-usage)

```
use b2r\Component\Twig\Twig;

$twig = new Twig();

echo $twig
    ->template('hello', 'Hello, {{ name }}') // Define 'hello' template and 'hello' as current template
    ->name('world') // Set name context
    ; #=>'Hello, world' invoke __toString()
```

With template, filter, function, global
---------------------------------------

[](#with-template-filter-function-global)

```
use b2r\Component\Twig\Twig;

$twig = new Twig(__DIR__ . '/templates');

$twig->addSuffix('.twig');

$twig->addFilters([
    'l' => function ($value) {
        return strtolower($value);
    },
    'u' => function ($value) {
        return strtoupper($value);
    },
]);

$twig->addFunctions([
    'x2' => function ($value) {
        return $value * 2;
    },
]);

$twig->addGlobals([
    'foo' => 'FOO',
    'bar' => 'BAR',
    'baz' => 'BAZ',
]);

echo $twig->template('hello')->name('world');
```

**hello.twig**

```
Hello, {{ name }}
Hello, {{ name|u }}
Hello, {{ name|l }}
Hello, {{ foo }}
Hello, {{ bar }}
Hello, {{ baz }}
Score: {{ x2(100) }}

```

**outputs**

```
Hello, world
Hello, WORLD
Hello, world
Hello, FOO
Hello, BAR
Hello, BAZ
Score: 200

```

Docs
----

[](#docs)

- [Twig](docs/Twig.md)

###  Health Score

24

—

LowBetter than 31% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity50

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

Every ~7 days

Total

3

Last Release

3376d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/74344?v=4)[b2r](/maintainers/b2r)[@b2r](https://github.com/b2r)

---

Top Contributors

[![b2r](https://avatars.githubusercontent.com/u/74344?v=4)](https://github.com/b2r "b2r (27 commits)")

---

Tags

twigtemplate

### Embed Badge

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

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

###  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

36667.2M319](/packages/twig-intl-extra)[symfony/ux-twig-component

Twig components for Symfony

21917.2M304](/packages/symfony-ux-twig-component)[symfony/ux-live-component

Live components for Symfony

1636.5M113](/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

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

PHPackages © 2026

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