diff --git a/LICENSE b/LICENSE index 39737a6..ecce590 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2014-2023 Eugene Leonovich +Copyright (c) 2014-2024 Eugene Leonovich Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/src/DeferredExtension.php b/src/DeferredExtension.php index aa70449..36cb1eb 100644 --- a/src/DeferredExtension.php +++ b/src/DeferredExtension.php @@ -19,6 +19,7 @@ final class DeferredExtension extends AbstractExtension { private $blocks = []; + private $clearBlocksOnBufferCleanup = true; public function getTokenParsers() : array { @@ -37,7 +38,9 @@ public function defer(Template $template, string $blockName) : void $index = \count($this->blocks[$templateName]) - 1; \ob_start(function (string $buffer) use ($index, $templateName) { - unset($this->blocks[$templateName][$index]); + if ($this->clearBlocksOnBufferCleanup) { + unset($this->blocks[$templateName][$index]); + } return $buffer; }); @@ -51,7 +54,9 @@ public function resolve(Template $template, array $context, array $blocks) : voi } while ($blockName = \array_pop($this->blocks[$templateName])) { + $this->clearBlocksOnBufferCleanup = false; $buffer = \ob_get_clean(); + $this->clearBlocksOnBufferCleanup = true; $blocks[$blockName] = [$template, 'block_'.$blockName.'_deferred']; $template->displayBlock($blockName, $context, $blocks); diff --git a/tests/Fixtures/inherited.test b/tests/Fixtures/inherited.test new file mode 100644 index 0000000..1c9b2bc --- /dev/null +++ b/tests/Fixtures/inherited.test @@ -0,0 +1,12 @@ +--TEST-- +inherited +--TEMPLATE-- +{% extends "layout.twig" %} +{% block bar deferred %}[bar-overridden]{% endblock %} +--TEMPLATE(layout.twig)-- +{% block foo deferred %}[foo]{% endblock %} +{% block bar deferred %}[bar]{% endblock %} +--DATA-- +return [] +--EXPECT-- +[bar-overridden][foo] diff --git a/tests/IntegrationTest.php b/tests/IntegrationTest.php index b05ba37..2d26dc4 100644 --- a/tests/IntegrationTest.php +++ b/tests/IntegrationTest.php @@ -56,11 +56,7 @@ public function testDeferredBlocksAreClearedOnRenderingError() : void ', ]); - $twig = new Environment($loader, [ - 'cache' => false, - 'strict_variables' => true, - ]); - + $twig = new Environment($loader); $twig->addExtension(new DeferredExtension()); $twig->addFunction(new TwigFunction('error', static function () { throw new \RuntimeException('Oops');