PHPackages                             filhocodes/twig-stack-extension - 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. filhocodes/twig-stack-extension

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

filhocodes/twig-stack-extension
===============================

Implement content stacks into Twig templates.

v2.0.0(4y ago)95.5k↓33.3%4[1 issues](https://github.com/filhocodes/twig-stack-extension/issues)[2 PRs](https://github.com/filhocodes/twig-stack-extension/pulls)MITPHPPHP ^7.4|^8.0

Since Feb 24Pushed 2y ago1 watchersCompare

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

READMEChangelog (2)Dependencies (3)Versions (3)Used By (0)

Twig Stack Extension
====================

[](#twig-stack-extension)

[![Composer Package](https://camo.githubusercontent.com/8e5a2014af0d98cb3e95575787fbe8aa912c12951e73021a4fc5003b26925aec/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f66696c686f636f6465732f747769672d737461636b2d657874656e73696f6e)](https://packagist.org/packages/filhocodes/twig-stack-extension)[![Composer Package Downloads](https://camo.githubusercontent.com/d122f4e66fd393a730ed580ec51a03613d885eb5ea566fe0b01bf0154d228c69/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f66696c686f636f6465732f747769672d737461636b2d657874656e73696f6e)](https://packagist.org/packages/filhocodes/twig-stack-extension)[![Build Status](https://github.com/filhocodes/twig-stack-extension/actions/workflows/tests.yml/badge.svg)](https://github.com/filhocodes/twig-stack-extension/actions)[![Coverage Status](https://camo.githubusercontent.com/333a1bea22d2a67fdfda483efec618e3628fbdf796ff6f05a5f73b4d860194c9/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f66696c686f636f6465732f747769672d737461636b2d657874656e73696f6e2f62616467652e737667)](https://coveralls.io/github/filhocodes/twig-stack-extension)

This extension allows you to define a section of a "base template" that will receive contents from all the other pages, either via `include`, `embed` or `extends`.

The reason behind this package is that `phive/twig-extensions-deferred` doesn't really have the features I wanted. For those who doesn't know, this extension allows you to say that a block in your template will be evaluated at the end of the template, and not while it is parsing. It also serves as a learning project, since it is my first Twig Extension.

This project is also HEAVILY related to the package `aygon/twig-stack`. It's kinda like a refactor of their code into Twig 2.\*, with my opinionated choices.

CHANGELOG v2.0.0
----------------

[](#changelog-v200)

Version 2.0.0 enables support for both PHP 8.0 and PHP 8.1, while removing support for PHP 7.3 and bellow. If you are still using those old PHP versions, you will need to install the version 1.0.0 of this package.

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

[](#installation)

You can install this package via composer:

```
composer require filhocodes/twig-stack-extension
```

If you are using Symfony, you should be able to configure it by adding a entry into `services.yaml`:

```
services:
    FilhoCodes\TwigStackExtension\TwigStackExtension:
        class: 'FilhoCodes\TwigStackExtension\TwigStackExtension'
        tags: ['twig.extension']
```

In either way, you can always add the extension directly into your Twig environment:

```
use FilhoCodes\TwigStackExtension\TwigStackExtension;

/** @var $twig \Twig\Environment */
$twig->addExtension(new TwigStackExtension());
```

Usage
-----

[](#usage)

In your base template, define a stack using `{% stack %}`

```

  My template
  {% stack styles %}

  {% stack scripts %}

```

Then, in files that either extend your template, or are included by any other, you may use `{% push %}` and `{% prepend %}` to add contents to the end and the beginning of the stack, respectively.

```
{% extends 'base.twig.html' %}

{% push scripts %}

{% endpush %}
```

```
{# include 'partial.twig.html' #}

{% push styles %}

{% endpush %}
```

To prevent duplicated code, like in files that are included or embed multiple times you can add an "id" to the block declaration in order to the extension to identify the block as unique. Just keep in mind that we will not verify the contents of said block, neither if it is a `push` or `prepend`. So watch out for possible collision-naming bugs.

```
{# embed 'component.twig.html' #}

{# The following contents will be rendered only once #}
{% push scripts component_definition_script %}

  MyPlugin = { /* ... */ }

{% endpush %}

{# The following contents will be rendered as many times as this file is embed,
   probably each with some different initialization logic... #}
{% push scripts %}

  MyPlugin.init();

{% endpush %}
```

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity30

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity62

Established project with proven stability

 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 ~676 days

Total

2

Last Release

1590d ago

Major Versions

v1.0.0 → v2.0.02021-12-31

PHP version history (2 changes)v1.0.0PHP ^7.2.5

v2.0.0PHP ^7.4|^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/5cc209810b516ba21283ceb79a3f59064948589bb5cb93f6f0680932145d5c38?d=identicon)[marcossffilho](/maintainers/marcossffilho)

---

Top Contributors

[![filhocodes](https://avatars.githubusercontent.com/u/4489182?v=4)](https://github.com/filhocodes "filhocodes (8 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/filhocodes-twig-stack-extension/health.svg)

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

###  Alternatives

[twig/extra-bundle

A Symfony bundle for extra Twig extensions

91292.0M313](/packages/twig-extra-bundle)[twig/intl-extra

A Twig extension for Intl

36663.2M218](/packages/twig-intl-extra)[rcrowe/twigbridge

Adds the power of Twig to Laravel

9105.9M50](/packages/rcrowe-twigbridge)[twig/string-extra

A Twig extension for Symfony String

21946.0M132](/packages/twig-string-extra)[twig/cssinliner-extra

A Twig extension to allow inlining CSS

23018.5M53](/packages/twig-cssinliner-extra)[symfony/ux-twig-component

Twig components for Symfony

21814.8M161](/packages/symfony-ux-twig-component)

PHPackages © 2026

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