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

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

mediagone/twig-powerpack
========================

Provides code-quality helpers to your Twig templates.

0.2.5(5y ago)41.4k1MITPHPPHP ^7.4|^8.0

Since Jan 2Pushed 5y ago1 watchersCompare

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

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

Twig PowerPack
==============

[](#twig-powerpack)

⚠️ This project is in experimental phase.

[![Latest Version on Packagist](https://camo.githubusercontent.com/94b2baa30b696398155791a23a9e74a15ba01ae9d6f525a75e5623ded3afa820/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d65646961676f6e652f747769672d706f7765727061636b2e737667)](https://packagist.org/packages/mediagone/twig-powerpack)[![Total Downloads](https://camo.githubusercontent.com/7b965ee41a732fddce5d9a7d53df2fb00eff1155c638f0f8fa2346003b8e0cc5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d65646961676f6e652f747769672d706f7765727061636b2e737667)](https://packagist.org/packages/mediagone/twig-powerpack)[![Software License](https://camo.githubusercontent.com/074b89bca64d3edc93a1db6c7e3b1636b874540ba91d66367c0e5e354c56d0ea/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e737667)](LICENSE)

This package provides code-quality features to your Twig templates:

1. [Type-safety checks for template context variables](#feat1).
2. [Register global data/resources from any template](#feat2).
3. [Instantiate classes in templates](#feat3).

Along with new functionalities:

- `|json_decode` filter

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

[](#installation)

This package requires **PHP 7.4+** and **Twig 2+**.

Add it as Composer dependency:

```
$ composer require mediagone/twig-powerpack
```

If you're using Symfony, enable the extension in `services.yaml`:

```
services:

    Mediagone\Twig\PowerPack\TwigPowerPackExtension:
        tags: [twig.extension]
```

Introduction
------------

[](#introduction)

Twig templating engine is seriously lacking types....

Features
--------

[](#features)

### 1) Context Variables type-checking

[](#1-context-variables-type-checking)

Templates usually require specific external data, but there is no native way to check the type of supplied variables. The `expect` tag allows you to **declare required variables** in your Twig files, making them also **self-documenting**. If the data is invalid, an exception will be thrown.

#### Primitive types

[](#primitive-types)

Supported scalar types are: *bool, float, int* and *string*.

```
{% extends 'layout.twig' %}

{% expect 'string' as TITLE %}
{% expect 'bool' as ENABLED %}
{% expect 'float' as AMOUNT %}
{% expect 'int' as COUNT %}
```

*Note: TITLE, ENABLED, AMOUNT and COUNT represent the names of required variables.*

#### Objects

[](#objects)

Because they don't guarantee any data structure, anonymous objects (*stdClass*) are not supported. However, usage of named classes is strongly encouraged to expose data in your templates. Therefore, a *Fully Qualified Class Name* (FQCN) can also be supplied:

```
{% expect 'App\\UI\\ViewModels\\Foo' as FOO %}

{{ FOO.bar }}
```

#### Nullable

[](#nullable)

Sometimes, you may want to ensure that a variable is defined while making it optional by using the `nullable` keyword:

```
{% expect nullable 'App\\UI\\ViewModels\\Foo' as FOO %}

{% if FOO != null %}
...
{% endif %}
```

#### Arrays

[](#arrays)

You can also check if a variable is an array of a given type by using the `array of` keywords:

```
{% expect array of 'App\\UI\\ViewModels\\Foo' as ARRAY %}

{% for foo in ARRAY %}
...
{% endfor %}
```

Arrays can also be nullable:

```
{% expect nullable array of 'App\\UI\\ViewModels\\Foo' as ARRAY %}

{% if ARRAY != null %}
...
{% endif %}
```

Or contain nullable elements:

```
{% expect array of nullable 'App\\UI\\ViewModels\\Foo' as ARRAY %}

{% for foo in ARRAY %}
    {% if foo != null %}
    ...
    {% endif %}
{% endfor %}
```

And even nullable array of nullable elements!

```
{% expect nullable array of nullable 'App\\UI\\ViewModels\\Foo' as ARRAY %}
```

*Note: Checking array's items type might induce a slight overhead, but unless you have thousands of elements it should be negligible.*

---

### 2) Register global data from any template

[](#2-register-global-data-from-any-template)

You may occasionally declare specific data in your templates, used in the global scope. For example if your templates dynamically add CSS classes to HTML body, or if they require optional CSS or JavaScript resources you only want to include on demand.

#### String Data

[](#string-data)

Short string data can be registered from anywhere in your templates using the `{% register  in  %}` tag:

```
// Page.twig

{% extends 'Layout.twig' %}

{% register 'has-menu' in 'bodyClasses' %}
{% register 'responsive' in 'bodyClasses' %}

{% register '/css/few-styles.css' in 'styles' %}
{% register '/css/some-styles.css' in 'styles' %}

{% register '/js/custom-scripts.js' in 'scripts' %}

...
```

And retrieved elsewhere through the `registry()` function:

```
// Layout.twig

        ...

        {% for css in registry('styles') %}

        {% endfor %}

        ...

        {% for js in registry('scripts') %}

        {% endfor %}

```

#### Optional registry clause

[](#optional-registry-clause)

For convenience, the registry name can be automatically inferred from the data when it represents a *path with an extension*, making usage of `in ` optional. The following lines are equivalent:

```
{% register '/styles.css' in 'css' %}
{% register '/styles.css' %}
```

#### Body Data

[](#body-data)

Because you may need longer or dynamically generated data, the tag also supports a block syntax to allow a content body to be provided. In this case you *cannot* define data in the opening tag and *the registry clause is mandatory*: `{% register in  %}  {% endregister %}`

For example if you want to declare inline scripts from a template:

```
// Page.twig
{% extends 'Layout.twig' %}

{% set name = 'world' %}

{% register in 'inlineJs' %}
    alert('Hello {{ name }}');
{% endregister %}
```

And include it at the end of the html page:

```
// Layout.twig

        ...

        {% for js in registry('inlineJs') %}
            {{ js|raw }}
        {% endfor %}

```

#### Unicity

[](#unicity)

Data can be declared as unique, so if multiple templates register the same value, it will be included only once. It's required most of the time, just add the `once` keyword to the tag:

```
{% register once '/styles.css' %}

// Subsequent identical statements will be ignored
{% register once '/styles.css' %}
```

It also works with body data:

```
{% register once '/styles.css' %}
{% register once in 'css' %}/styles.css{% register %}  // ignored
```

However, unicity is only enforced **within the same registry**, so both following statements will be taken into account:

```
{% register once '/styles.css' in 'css' %}
{% register once '/styles.css' in 'styles' %}
```

#### Priority

[](#priority)

As you cannot always predict in which order data will be registered, you'll sometime need to ensure a data comes first, for example in the case of a script library required by others. Then, add the `priority` keyword at the end of your tag followed by a priority number (lower values come first\*).

Tags without priority always come after prioritized ones.

*Note: the order of data with the same priority (or undefined) is not guaranteed.*

```
{% register '/last.js' %}
{% register '/second.js' priority 2 %}
{% register '/first.js' priority 1 %}

```

---

### 3) Instantiate classes in templates

[](#3-instantiate-classes-in-templates)

Although it's better to do it in the controller when possible, you may need to create class instances directly in a template. The `new(string $fqcn, ...$args)` function allows you to call the constructor of a given class:

```
{% include('Partials/Menu.twig') with {Menu: new('App\\UI\\Partials\\Menu',
    'Main menu',
    [
        {Label: 'Item 1', Href: '/url/to/item1'},
        {Label: 'Item 2', Href: '/url/to/item2'},
    ],
)} %}
```

Given the following View Model class:

```
namespace App\UI\Partials;

final class Menu
{
    private string $name;
    private array $items;

    public function __construct(string $name, array $items)
    {
        $this->name = $name;
        $this->items = array_map(static fn($item) => new MenuItem($item), $items);
    }
}
```

License
-------

[](#license)

*Twig PowerPack* is licensed under MIT license. See LICENSE file.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community9

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

Every ~10 days

Recently: every ~22 days

Total

10

Last Release

1854d ago

PHP version history (2 changes)0.1.0PHP ^7.4

0.2.4PHP ^7.4|^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/ce000f43f69a7dc38a80da57d86a712a3e27f86b5fd4090f024472a03b28a089?d=identicon)[Mediagone](/maintainers/Mediagone)

---

Top Contributors

[![Mediagone](https://avatars.githubusercontent.com/u/32240357?v=4)](https://github.com/Mediagone "Mediagone (38 commits)")

---

Tags

clean-codetemplatingtwigview-modeltwigqualityextension

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[shoot/shoot

Shoot aims to make providing data to your templates more manageable

41229.9k2](/packages/shoot-shoot)[redant/twig-components

Define reusable components in Twig.

3819.8k1](/packages/redant-twig-components)[umanit/twig-image-extension

An extension to facilitate the integration of responsive images' markup in Twig templates.

1042.2k](/packages/umanit-twig-image-extension)

PHPackages © 2026

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