Skip to content

Commit

Permalink
Fix overridden blocks are not rendered (#19)
Browse files Browse the repository at this point in the history
See #17.
  • Loading branch information
rybakit committed Apr 4, 2024
1 parent 13a70ae commit 3e5d00c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 6 additions & 1 deletion src/DeferredExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
final class DeferredExtension extends AbstractExtension
{
private $blocks = [];
private $clearBlocksOnBufferCleanup = true;

public function getTokenParsers() : array
{
Expand All @@ -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;
});
Expand All @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions tests/Fixtures/inherited.test
Original file line number Diff line number Diff line change
@@ -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]
6 changes: 1 addition & 5 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 3e5d00c

Please sign in to comment.