PHPackages                             namics/twig-nitro-library - 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. namics/twig-nitro-library

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

namics/twig-nitro-library
=========================

Extension to embrace the Terrific frontend methodology in Twig. Currently it adds a custom component tag to Twig which mimics Nitro's handlebars helper.

v4.1.0(1y ago)812.1k↑26.2%4[6 issues](https://github.com/namics/twig-nitro-library/issues)1MITPHPPHP &gt;=8.0CI failing

Since May 23Pushed 1y ago5 watchersCompare

[ Source](https://github.com/namics/twig-nitro-library)[ Packagist](https://packagist.org/packages/namics/twig-nitro-library)[ Docs](https://github.com/namics/twig-nitro-library)[ RSS](/packages/namics-twig-nitro-library/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (8)Dependencies (2)Versions (11)Used By (1)

Terrific Twig
=============

[](#terrific-twig)

[![Build Status](https://github.com/namics/twig-nitro-library/workflows/Twig%20Nitro%20Library/badge.svg)](https://github.com/namics/twig-nitro-library/workflows/Twig%20Nitro%20Library/badge.svg)[![Latest version](https://camo.githubusercontent.com/78c8ac55b3c637d161d829948edddcc9accc633c651492a2191c9acbd6ea0656/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f6e616d6963732f747769672d6e6974726f2d6c696272617279)](https://camo.githubusercontent.com/78c8ac55b3c637d161d829948edddcc9accc633c651492a2191c9acbd6ea0656/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f6e616d6963732f747769672d6e6974726f2d6c696272617279)[![License](https://camo.githubusercontent.com/8c21aa0aa4c40fd641f0b791a498a69214659d3fce8faad47bf8ef8999bae254/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6e616d6963732f747769672d6e6974726f2d6c696272617279)](https://camo.githubusercontent.com/8c21aa0aa4c40fd641f0b791a498a69214659d3fce8faad47bf8ef8999bae254/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6e616d6963732f747769672d6e6974726f2d6c696272617279)[![Packagist PHP Version Support](https://camo.githubusercontent.com/1e9ef36858b411940addf8a1fcf0f9b12327a71a1b776e674cd721cdebccedba/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6e616d6963732f747769672d6e6974726f2d6c6962726172793f636f6c6f723d253233373837434235)](https://camo.githubusercontent.com/1e9ef36858b411940addf8a1fcf0f9b12327a71a1b776e674cd721cdebccedba/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6e616d6963732f747769672d6e6974726f2d6c6962726172793f636f6c6f723d253233373837434235)

Extension to embrace the [Terrific](https://github.com/brunschgi/terrificjs) frontend methodology in [Twig](http://twig.sensiolabs.org/).

Adds a custom `component` tag to Twig which mimics the [Nitro](https://github.com/namics/generator-nitro) handlebars helper.

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

[](#installation)

Using [composer](https://packagist.org/packages/namics/terrific-twig):

```
$ composer require namics/terrific-twig
```

Requirements
------------

[](#requirements)

The following versions of PHP are currently supported.

- 8.0
- 8.1

Setup
-----

[](#setup)

Step 1: Implement `TemplateInformationProvider`

```
class TemplateInformationProvider implements TemplateInformationProviderInterface
{
    public function getPaths() {
    /* List of path where Terrific Components can be found, e.g.
      @code
      [
        '/var/www/example.com/frontend/src/atoms',
        '/var/www/example.com/frontend/src/molecules',
        '/var/www/example.com/frontend/src/organisms'
      ]
      @endcode */
      return [];
    }
}
```

Step 2: Implement `ContextProviderInterface`

```
class ContextProvider implements ContextProviderInterface
{
    public function compile(\Twig\Compiler $compiler, \Twig\Node\Node $component, \Twig\Node\Node $dataVariant, $only) {
        // ...
    }
}
```

Step 3: Add `TerrificLoader`

```
$loader = ...;
$chain = new \Twig\Loader\ChainLoader([$loader, new TerrificLoader(new TemplateInformationProvider)]);
$twig = new \Twig\Environment($chain);
```

Step 4: Add `TerrificExtension`

```
$twig = new \Twig\Environment($chain);
$twig->addExtension(new TerrificExtension(new ContextProvider));
```

Step 5: Profit!

Usage
-----

[](#usage)

```
{# Includes the component, data object is merged with the context #}
{% component 'Example' { title: 'Inject an Object' } %}

{# Includes the component, data object is injected as a child context #}
{% component 'Example' { title: 'Inject an Object' } only %}

{# Includes the component, object variable data is merged with the context #}
{% set fooComponentData = { title: 'Inject an Object' } %}
{% component 'Example' fooComponentData %}

{# Includes the component with name contained by string variable, data object is merged with the context #}
{% set fooComponentName = 'Example' %}
{% component fooComponentName { title: 'Inject an Object' } %}

{# Includes the component with name contained by string variable, object variable data is merged with the context #}
{% set fooComponentName = 'Example' %}
{% set fooComponentData = { title: 'Inject an Object' } %}
{% component fooComponentName fooComponentData %}
```

Documentation
-------------

[](#documentation)

### Extension

[](#extension)

The extension provides terrific extensions to Twig. Currently, the extension provides the `ComponentTokenParser`.

### Token Parser

[](#token-parser)

The token parser contains the parsing step for the component tag. It tokenizes the stream to different nodes (`component`, `data`) and an attribute (`only`).

The functionality is based on the fantastic `Twig_TokenParser_Include`.

### Node

[](#node)

The Node compiles the tokenized tag to PHP. To see some of the output, check the [`ComponentTest`](https://github.com/namics/terrific-twig/blob/master/test/Twig/Node/ComponentTest.php).

### Loader

[](#loader)

The `TerrificLoader` loads templates contained inside the given paths. An implementation of `TemplateLocatorInterface` provides the paths where the loader should search for templates. Recursively loads any directories contained inside the given directories.

### Template Information Provider

[](#template-information-provider)

An implementation of `TemplateInformationProviderInterface` should return a list of paths where templates live. These should be in the form of `['frontend/components/atoms', 'frontend/components/molecules', 'frontend/components/organisms']`. The component directory will be provided by the `TerrificLoader` (`Example/example.[ext]`).

### Context Provider

[](#context-provider)

This is the tricky part. An implementation of `ContextProviderInterface` decides which data will be made available to Twig templates. TODO: More on that.

### ConfigReader

[](#configreader)

Reads nitro's `config.json` and parses essential information such as the component paths and file extension.

### Tests

[](#tests)

Tests can be run by using the following command:

```
composer run-scrip tests
```

### CI

[](#ci)

This project uses GitHub actions.

#### Run locally

[](#run-locally)

- Install [nektos/act](https://github.com/nektos/act).
- Open terminal, go to project directory.
- Run `act -P ubuntu-latest=shivammathur/node:latest` as described [here](https://github.com/shivammathur/setup-php#local-testing-setup).

Credits
-------

[](#credits)

- [Twig Template Engine](http://twig.sensiolabs.org/)
- [Terrific](http://terrifically.org/)
- [Terrific Micro](https://github.com/namics/terrific-micro)
- [Terrific Micro Twig](https://github.com/namics/terrific-micro-twig)
- [Nitro](https://github.com/namics/generator-nitro)

This project is partially sponsored by [Namics](https://github.com/namics).

###  Health Score

40

—

FairBetter than 86% of packages

Maintenance22

Infrequent updates — may be unmaintained

Popularity31

Limited adoption so far

Community20

Small or concentrated contributor base

Maturity74

Established project with proven stability

 Bus Factor1

Top contributor holds 68.8% 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 ~346 days

Recently: every ~397 days

Total

10

Last Release

576d ago

Major Versions

v1.0.1 → v2.0.0-rc12020-07-30

2.0.0-rc2 → v3.0.02023-10-27

v3.0.1 → v4.0.02024-10-31

PHP version history (3 changes)v1.0.0-betaPHP &gt;=5.4

v2.0.0-rc1PHP &gt;=7.3

v3.0.0PHP &gt;=8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1849964?v=4)[Lulzim Bina](/maintainers/lulbina)[@lulbina](https://github.com/lulbina)

![](https://www.gravatar.com/avatar/461c748e44111932d7e84f02bc149e5cabefad089e46bce97ce6b883de6363f5?d=identicon)[namicsorg](/maintainers/namicsorg)

---

Top Contributors

[![deniaz](https://avatars.githubusercontent.com/u/698963?v=4)](https://github.com/deniaz "deniaz (11 commits)")[![orlandothoeny](https://avatars.githubusercontent.com/u/17168783?v=4)](https://github.com/orlandothoeny "orlandothoeny (2 commits)")[![rdamjanov](https://avatars.githubusercontent.com/u/4276637?v=4)](https://github.com/rdamjanov "rdamjanov (2 commits)")[![nemanjaparadina](https://avatars.githubusercontent.com/u/214821152?v=4)](https://github.com/nemanjaparadina "nemanjaparadina (1 commits)")

---

Tags

twigintegrationnitroterrificnamics

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/namics-twig-nitro-library/health.svg)

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

###  Alternatives

[symfony/ux-twig-component

Twig components for Symfony

22018.6M356](/packages/symfony-ux-twig-component)[symfony/ux-live-component

Live components for Symfony

1647.0M127](/packages/symfony-ux-live-component)[symfony/ux-toolkit

A tool to easily create a design system in your Symfony app with customizable, well-crafted Twig components

16126.1k1](/packages/symfony-ux-toolkit)[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)
