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

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

felds/twig-extra
================

Custom Twig tags and extensions (switch tag).

1.0.0(5mo ago)00MITPHPPHP &gt;=8.4CI passing

Since Jan 21Pushed 5mo agoCompare

[ Source](https://github.com/felds/twig-extra)[ Packagist](https://packagist.org/packages/felds/twig-extra)[ RSS](/packages/felds-twig-extra/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (2)Versions (2)Used By (0)

felds/twig-extra
================

[](#feldstwig-extra)

Custom Twig extras for Twig 3.22+ and PHP 8.4+:

- `switch` tag
- `?.` nullsafe operator

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

[](#installation)

```
composer require felds/twig-extra
```

Usage
-----

[](#usage)

Register the desired extensions with Twig:

```
$twig->addExtension(new \Felds\TwigExtra\SwitchExtension());
$twig->addExtension(new \Felds\TwigExtra\NullsafeExtension());
```

In Symfony, you can let autoconfigure pick it up by registering the services:

```
# services.yaml
Felds\TwigExtra\SwitchExtension: ~
Felds\TwigExtra\NullsafeExtension: ~
```

### Switch tag (`{% switch %}`)

[](#switch-tag--switch-)

Match a value against multiple comparisons, picking the first `case` that evaluates truthy:

```
{% switch value %}
{% case 1 %} exactly 1
{% case == 2 %} exactly 2
{% case > 1 %} More than 1
{% default %} Other
{% endswitch %}
```

- Supports all Twig comparison operators (==, !=, &gt;, &lt;, &gt;=, &lt;=, `is same as`, `in`, `not in`, `matches`, `starts with`, `ends with`, etc.).
- Cases are evaluated top-down; the first hit wins, otherwise `default` runs when present.
- The switch value is stored internally and the original context is restored after the block.

### Nullsafe operator (`?.`)

[](#nullsafe-operator-)

Access attributes and call methods without throwing when the left side is null or missing:

```
{{ user?.name }}
{{ post?.author?.getName() }}
{{ date?.format('Y-m-d') ?? 'no date' }}
```

- If the base value is null or undefined, the chain short-circuits to null.
- Works with arrays, objects, property fetches, and method calls.
- Respects strict variables (nullsafe short-circuits instead of raising).

Tests
-----

[](#tests)

```
composer install
./vendor/bin/phpunit tests
```

License
-------

[](#license)

MIT

Changelog
---------

[](#changelog)

See [CHANGELOG.md](CHANGELOG.md).

###  Health Score

34

—

LowBetter than 75% of packages

Maintenance71

Regular maintenance activity

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity52

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

163d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8ba8ce29057e7a9c7b35349a8f0c6ebd69dcfff3a0a6f14d006af46c1780d607?d=identicon)[felds](/maintainers/felds)

---

Top Contributors

[![felds](https://avatars.githubusercontent.com/u/139979?v=4)](https://github.com/felds "felds (5 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[craftcms/cms

Craft CMS

3.6k3.6M3.1k](/packages/craftcms-cms)[mati365/ckeditor5-symfony

CKEditor 5 integration for Symfony

262.6k](/packages/mati365-ckeditor5-symfony)

PHPackages © 2026

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