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

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

futureplc/twig-stack-extension
==============================

Adds the `push` and `stack` tags two twig for better asset management throughout templates.

v1.0.2(1y ago)12.7k↓34.4%1[2 PRs](https://github.com/futureplc/twig-stack-extension/pulls)MITPHPPHP ^8.3CI passing

Since Sep 2Pushed 4mo ago4 watchersCompare

[ Source](https://github.com/futureplc/twig-stack-extension)[ Packagist](https://packagist.org/packages/futureplc/twig-stack-extension)[ Docs](https://github.com/futureplc/twig-stack-extension)[ GitHub Sponsors](https://github.com/futureplc)[ RSS](/packages/futureplc-twig-stack-extension/feed)WikiDiscussions main Synced 1mo ago

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

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

[](#twig-stack-extension)

[![Latest Version on Packagist](https://camo.githubusercontent.com/21e576ddcdac60b68c07c06c3f05495263a7b4e781d1e1e80004687376a3a127/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f667574757265706c632f747769672d737461636b2d657874656e73696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/futureplc/twig-stack-extension)[![Tests](https://camo.githubusercontent.com/72544d71dafe10dd1edd818176f3eba6f9bc8cc2fa2cd7a4d913044d63f8281d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f667574757265706c632f747769672d737461636b2d657874656e73696f6e2f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/futureplc/twig-stack-extension/actions/workflows/run-tests.yml)[![Total Downloads](https://camo.githubusercontent.com/ae3007f9717c01febb77d6f96613f9e958b67519a3eadb9f0d7cc6f2679696bf/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f667574757265706c632f747769672d737461636b2d657874656e73696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/futureplc/twig-stack-extension)

This Twig extension allows you to "push" content from child views into a named "stack" which can be rendered in another view or layout. This is perfect for many scenarios, like pushing scripts or styles used by components.

This plugin is heavily inspired by [filhocodes/twig-stack-extension](https://github.com/filhocodes/twig-stack-extension) and refactored to work with Twig `^3.9.0`.

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

[](#installation)

You can install the package via Composer:

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

### Setup in your project

[](#setup-in-your-project)

First, add the extension to the Twig instance:

```
$twig->addExtension(new \Future\TwigStackExtension\StackExtension());
```

Next, we need to modify the output of the content rendered by Twig in order to ensure the stacks are injected into the right place properly.

One way this can be done is by using the custom `Environment` class provided by the package.

```
- use Twig\Environment;
+ use Future\TwigStackExtension\Environment;

// ...

$twig = new Environment($loader);
```

If you don't want to override your `Environment`, you can manually call the extension on the rendered output.

```
$result = $twig->render('view.html.twig');

$result = $twig
    ->getExtension(\Future\TwigStackExtension\StackExtension::class)
    ->getStackManager()
    ->replaceStackPlaceholdersWithStackContent($result);
```

It's all set up and ready to be used.

Usage
-----

[](#usage)

In Twig templates, you will have three new tags to use, `{% push %}` and `{% pushonce %}` to push content to a named stack, and `{% stack %}` to render the named stack.

### Pushing to a stack

[](#pushing-to-a-stack)

Wrap any content you want "pushed" to the stack each time this part of the Twig template is parsed.

```

{% push 'scripts' %}
    console.log('Pushed script executed')
{% endpush %}
```

Using `{% push %}` will push the contents to the stack *every time* it is called.

If you want to push only the first time the code is referenced, for example, including a library needed for a component, you can use the `{% pushonce %}` tag instead.

```

{% pushonce 'scripts' %}

{% endpushonce %}
```

### Rendering a stack

[](#rendering-a-stack)

You can use the `{% stack %}` tag to render the contents of the stack.

```

    ...

        ...
        {% stack 'scripts' %}

```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Chris Powell](https://github.com/ampedweb)
- [Marcos SF Filho](https://github.com/filhocodes)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

43

—

FairBetter than 91% of packages

Maintenance64

Regular maintenance activity

Popularity24

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 76.1% 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 ~117 days

Total

3

Last Release

390d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3e8a14b9f997cf85aacea7d39da9dc33c38cc05fe03360578327ea9bcb25f4d9?d=identicon)[ImLiam](/maintainers/ImLiam)

![](https://www.gravatar.com/avatar/1f5c9f4e29c7fe22627c108733402daff5181384f6f62fcb9ab966afdd1cf342?d=identicon)[ampedweb](/maintainers/ampedweb)

---

Top Contributors

[![ampedweb](https://avatars.githubusercontent.com/u/22029493?v=4)](https://github.com/ampedweb "ampedweb (35 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (6 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (3 commits)")[![imliam](https://avatars.githubusercontent.com/u/4326337?v=4)](https://github.com/imliam "imliam (2 commits)")

---

Tags

futureplctwig-stack-extension

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

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

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

###  Alternatives

[twig/extra-bundle

A Symfony bundle for extra Twig extensions

91492.0M315](/packages/twig-extra-bundle)[twig/intl-extra

A Twig extension for Intl

36763.2M221](/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

22046.0M133](/packages/twig-string-extra)[twig/cssinliner-extra

A Twig extension to allow inlining CSS

22918.5M55](/packages/twig-cssinliner-extra)[symfony/ux-twig-component

Twig components for Symfony

21914.8M162](/packages/symfony-ux-twig-component)

PHPackages © 2026

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