From d6b81f907448c428cd0aa05eab087e4655db5869 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 14 Sep 2026 11:08:39 +0200 Subject: [PATCH] Allow block chains to be composed of other block chains --- doc/api.rst | 12 ++++ src/BlockChain.php | 66 ++++++++++++++++++---- tests/BlockChainTest.php | 117 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 181 insertions(+), 14 deletions(-) diff --git a/doc/api.rst b/doc/api.rst index d910530ae..a58afb2b7 100644 --- a/doc/api.rst +++ b/doc/api.rst @@ -126,6 +126,18 @@ default variables for that resolution, which the render context can override:: Pass the same context to ``hasBlock()``, ``getBlockNames()`` and ``renderBlock()`` to keep them consistent. +A chain also accepts other chains:: + + $base = new BlockChain($twig, ['base_theme.html.twig']); + $application = new BlockChain($twig, ['application_theme.html.twig', $base]); + $admin = new BlockChain($twig, ['admin_theme.html.twig', $application]); + +A chained chain keeps its position in the precedence order: templates listed +before it win, and it wins over templates listed after it. It contributes the +blocks it composes itself, resolved with its own default variables. Chaining a +chain never modifies it, so the same chain can be reused in as many others as +needed. + Streaming Templates ------------------- diff --git a/src/BlockChain.php b/src/BlockChain.php index e68f145da..81d0a7105 100644 --- a/src/BlockChain.php +++ b/src/BlockChain.php @@ -18,10 +18,10 @@ use Twig\Error\RuntimeError; */ final class BlockChain { - /** @var list