Download the PHP package filhocodes/twig-stack-extension without Composer
On this page you can find all versions of the php package filhocodes/twig-stack-extension. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download filhocodes/twig-stack-extension
More information about filhocodes/twig-stack-extension
Files in filhocodes/twig-stack-extension
Package twig-stack-extension
Short Description Implement content stacks into Twig templates.
License MIT
Informations about the package twig-stack-extension
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
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
You can install this package via composer:
If you are using Symfony, you should be able to configure it by adding a entry
into services.yaml
:
In either way, you can always add the extension directly into your Twig environment:
Usage
In your base template, define a stack using {% stack %}
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.
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.