PHPackages                             ju1ius/twig-buffers-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. ju1ius/twig-buffers-extension

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

ju1ius/twig-buffers-extension
=============================

Like Blade stacks? Use Twig buffers!

v0.8.2(1y ago)0375MITPHPPHP &gt;=8.1

Since Nov 1Pushed 1y ago1 watchersCompare

[ Source](https://github.com/ju1ius/twig-buffers-extension)[ Packagist](https://packagist.org/packages/ju1ius/twig-buffers-extension)[ RSS](/packages/ju1ius-twig-buffers-extension/feed)WikiDiscussions main Synced today

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

Twig Buffers Extension
======================

[](#twig-buffers-extension)

[![codecov](https://camo.githubusercontent.com/2ecf8c8c550599abba831d70b1afac83aec5fecd1975a99d3c517f8297df5061/68747470733a2f2f636f6465636f762e696f2f67682f6a75316975732f747769672d627566666572732d657874656e73696f6e2f6272616e63682f6d61696e2f67726170682f62616467652e7376673f746f6b656e3d30465049585258553436)](https://codecov.io/gh/ju1ius/twig-buffers-extension)

This extension allows you to define buffers into which you can insert content from another part of the template or from included, embedded or child templates. It is similar in functionality to [Blade stacks](https://laravel.com/docs/master/blade#stacks).

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

[](#installation)

```
composer require ju1ius/twig-buffers-extension
```

```
use Twig\Environment;
use ju1ius\TwigBuffersExtension\TwigBuffersExtension;

$twig = new Environment();
$twig->addExtension(new TwigBuffersExtension());
```

Basic usage
-----------

[](#basic-usage)

Let's start with what will probably be the most frequent use case.

Given the following templates:

```
{# base.html.twig #}

  {% buffer stylesheets %}

    {% block content '' %}

  {% buffer scripts %}

```

```
{# page.html.twig #}

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

{% block content %}

  {{ greeting }}

  {% append to stylesheets %}

  {% endappend %}
  {% append to scripts %}

  {% endappend %}

{% endblock content %}
```

Rendering page.html.twig

```
$twig->display('page.html.twig', ['greeting' => 'Hello buffers!']);
```

Will output:

```

    Hello buffers!

```

The buffer tag
--------------

[](#the-buffer-tag)

The buffer tag does two things:

- it opens a named buffer if a buffer of the same name doesn't exist.
- it references the named buffer so that it's content can be displayed at the tag's location.

A named buffer can therefore be referenced several times:

```
{% buffer my_buffer %}
{% buffer my_buffer %}
{% append to my_buffer 'bar' %}
```

```
bar
bar
```

By default, the contents of the buffer are joined using the empty string, but you can customize the joining string:

```
{% buffer my_buffer joined by ', ' %}
{% append to my_buffer 'foo' %}
{% append to my_buffer 'bar' %}
{% append to my_buffer 'baz' %}
```

```
foo, bar, baz
```

You can also provide a «final glue», similarly to the `join` twig filter:

```
{% buffer my_buffer joined by '', '' %}
{% append to my_buffer 'foo' %}
{% append to my_buffer 'bar' %}
{% append to my_buffer 'baz' %}
```

```
foobarbaz
```

As we just saw, even when using automatic escaping, the joining strings *will not be automatically escaped*. You'll have to escape them yourself if they come from untrusted sources:

```
{% buffer my_buffer joined by some_variable|escape('html') %}
```

Inserting content into buffers
------------------------------

[](#inserting-content-into-buffers)

This is done via the `append` and `prepend` tags. They share the same syntax apart from their respective tag names.

As with the `block` tag, you can use a short or a long syntax:

```
{# short syntax #}
{% append to my_buffer 'Some content' %}
{# long syntax #}
{% append to my_buffer %}
  Some other content
{% endappend %}
```

As the name implies, the `append` tag appends content to the buffer.

The `prepend` tag however, doesn't prepend content to the buffer, but instead *appends content to the head of the buffer*:

```
{% buffer my_buffer %}
{% append to my_buffer '1' %}
{% append to my_buffer '2' %}
{% prepend to my_buffer '3' %}
{% prepend to my_buffer '4' %}
```

```
3412
```

Trying to insert content into a buffer that does not exist or is not [in scope](#the-scope-of-a-buffer) will throw an `UnknownBuffer` exception.

### Inserting to potentially undefined buffers

[](#inserting-to-potentially-undefined-buffers)

If you want to insert content into a buffer that may not exist or may not be [in scope](#the-scope-of-a-buffer), you have two solutions:

1. ```
    {% append or ignore to my_buffer '...' %}
    ```

    If `my_buffer` doesn't exist or is not [in scope](#the-scope-of-a-buffer), this is a no-op.
2. ```
    {% append or create to my_buffer '...' %}
    ```

    If `my_buffer` doesn't exist or is not [in scope](#the-scope-of-a-buffer), first open the buffer, then insert content into it.

### Unique insertions

[](#unique-insertions)

You can tag insertions with a unique id in order to prevent the same content to be inserted more than once.

```
{% buffer my_buffer %}

{% for character in 'A'..'Z' %}
  {% append to my_buffer as some_unique_id character %}
{% endfor %}
```

```
A
```

Clearing the contents of a buffer
---------------------------------

[](#clearing-the-contents-of-a-buffer)

You can use the `clear_buffer` function:

```
{% buffer my_buffer %}
{% append to my_buffer 'some content' %}
{% do clear_buffer('my_buffer') %}
```

Attempting to clear the content of a buffer that does not exist or is not [in scope](#the-scope-of-a-buffer) will throw an `UnknownBuffer` exception.

Checking if a buffer exists
---------------------------

[](#checking-if-a-buffer-exists)

You can use the `buffer` test:

```
{% if 'my_buffer' is buffer %}
  {% append to my_buffer 'some content' %}
{% endif %}
```

The `buffer` test will return `true` if the buffer exists and is [in scope](#the-scope-of-a-buffer).

Checking if a buffer is empty
-----------------------------

[](#checking-if-a-buffer-is-empty)

You can use the `empty_buffer` test:

```
{% buffer my_buffer %}
My buffer is {{ 'my_buffer' is empty_buffer ? 'empty' : 'not empty' }}!
```

Or for a more practical example:

```
{% if 'my_buffer' is not empty_buffer %}

    {% buffer my_buffer %}

{% endif %}
```

The `empty_buffer` test will return `true` if:

- the buffer exists and is empty
- the buffer doesn't exist or is not [in scope](#the-scope-of-a-buffer).

The scope of a buffer
---------------------

[](#the-scope-of-a-buffer)

When a template contains a `{% buffer %}` tag, the corresponding buffers are opened as soon as the template begins to render.

Once opened, a buffer remains available until the end of the topmost render call.

Therefore, a buffer is available:

1. in the whole template it is referenced in and in all it's included, embedded or child templates.
2. in all subsequent siblings of the template it is referenced in.

To clarify, lets look at some examples.

The following works because of rule n°1:

```
{% append to my_buffer '' %}
{% buffer my_buffer %}
```

The following also works because of rule n°1:

```
{# partial: insert-into-buffer.html.twig #}
{% append to my_buffer 'some content' %}
```

```
{% include 'insert-into-buffer.html.twig' %}
{% buffer my_buffer %}
```

The following works because of rule n°2:

```
{# partial: reference-buffer.html.twig #}
{% buffer my_buffer %}
```

```
{% include 'reference-buffer.html.twig' %}
{% append to my_buffer 'some content' %}
```

However, because of rule n°2, the following does not work:

```
{#
  This throws an `UnknownBuffer` exception beacause `my_buffer`
  is not in scope until the 'reference-buffer.html.twig'
  partial is included.
#}
{% append to my_buffer 'some content' %}
{% include 'reference-buffer.html.twig' %}
```

###  Health Score

32

—

LowBetter than 69% of packages

Maintenance39

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity56

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

Recently: every ~246 days

Total

12

Last Release

522d ago

PHP version history (2 changes)v0.1PHP &gt;=8.0

v0.7PHP &gt;=8.1

### Community

Maintainers

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

---

Top Contributors

[![ju1ius](https://avatars.githubusercontent.com/u/218404?v=4)](https://github.com/ju1ius "ju1ius (44 commits)")

---

Tags

bufferstacktwigtwig-extensiontwigstackBuffer

### Embed Badge

![Health badge](/badges/ju1ius-twig-buffers-extension/health.svg)

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

###  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)
