diff --git a/src/View/Antlers/Language/Runtime/NodeProcessor.php b/src/View/Antlers/Language/Runtime/NodeProcessor.php index b134341c7fb..6e0e677aff7 100644 --- a/src/View/Antlers/Language/Runtime/NodeProcessor.php +++ b/src/View/Antlers/Language/Runtime/NodeProcessor.php @@ -2557,7 +2557,10 @@ private function evaluatePhpBuffer(string $___phpBuffer, bool $___processAssignm $___antlersVarAfter = get_defined_vars(); foreach ($___antlersVarAfter as $___varKey => $___varValue) { - if (str_starts_with($___varKey, '___')) { + if ( + str_starts_with($___varKey, '___') || + (isset($___antlersVarBefore[$___varKey]) && $___antlersVarBefore[$___varKey] === $___varValue) + ) { continue; } diff --git a/tests/Antlers/Runtime/ParserIsolationTest.php b/tests/Antlers/Runtime/ParserIsolationTest.php index 90b5c162120..3025b456c4f 100644 --- a/tests/Antlers/Runtime/ParserIsolationTest.php +++ b/tests/Antlers/Runtime/ParserIsolationTest.php @@ -3,6 +3,7 @@ namespace Tests\Antlers\Runtime; use Facades\Tests\Factories\EntryFactory; +use Illuminate\Support\Str; use Statamic\Facades\Collection; use Statamic\Facades\Nav; use Statamic\Facades\Taxonomy; @@ -274,4 +275,47 @@ public function test_escaped_braces_in_concurrent_requests() $this->assertSame($expectedOne, $contentOne); $this->assertSame($expectedTwo, $contentTwo); } + + public function test_php_nodes_dont_overwrite_parent_scope_needlessly() + { + $this->createBlueprintsAndData(); + $this->withFakeViews(); + + $layout = <<<'EOT' +{{ template_content }} +Layout: {{ title }} +EOT; + + $template = <<<'EOT' +{{? + $loop_data = [ + [ + 'title' => 'A different title', + ], + ]; +?}} + +Before: {{ title }} +{{ collection:news }} + {{ collection:news }} + {{ loop_data }} + {{? /* */ ?}} + {{ /loop_data }} + {{ /collection:news }} +{{ /collection:news }} +After: {{ title }} +EOT; + + $this->viewShouldReturnRaw('layout', $layout); + $this->viewShouldReturnRaw('default', $template); + + $result = $this->get('bravo/news-2') + ->assertOk() + ->content(); + + $this->assertSame( + 'Before: News 2 After: News 2 Layout: News 2', + Str::squish($result) + ); + } }