diff --git a/src/Illuminate/View/Compilers/Concerns/CompilesIncludes.php b/src/Illuminate/View/Compilers/Concerns/CompilesIncludes.php index 647fe4578ced..bca0336fc051 100644 --- a/src/Illuminate/View/Compilers/Concerns/CompilesIncludes.php +++ b/src/Illuminate/View/Compilers/Concerns/CompilesIncludes.php @@ -79,4 +79,17 @@ protected function compileIncludeFirst($expression) return "first({$expression}, array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>"; } + + /** + * Compile the include-isolated statements into valid PHP. + * + * @param string $expression + * @return string + */ + protected function compileIncludeIsolated($expression) + { + $expression = $this->stripParentheses($expression); + + return "make({$expression})->render(); ?>"; + } } diff --git a/tests/Integration/View/BladeTest.php b/tests/Integration/View/BladeTest.php index 689093db3389..09cf6b34358c 100644 --- a/tests/Integration/View/BladeTest.php +++ b/tests/Integration/View/BladeTest.php @@ -225,6 +225,25 @@ public function testViewCacheCommandHandlesConfiguredBladeExtensions() $this->assertTrue($found); } + public function test_include_scoped_does_not_inherit_parent_scope() + { + // Regular @include passes parent scope variables + $regularInclude = View::make('uses-include-regular', [ + 'parentVar' => 'parent-value', + 'explicitVar' => 'explicit-value', + ])->render(); + + $this->assertSame('Parent: parent-value, Explicit: explicit-value', trim($regularInclude)); + + // @includeIsolated does NOT pass parent scope variables + $scopedInclude = View::make('uses-include-scoped', [ + 'parentVar' => 'parent-value', + 'explicitVar' => 'explicit-value', + ])->render(); + + $this->assertSame('Parent: undefined, Explicit: explicit-value', trim($scopedInclude)); + } + /** {@inheritdoc} */ #[\Override] protected function defineEnvironment($app) diff --git a/tests/Integration/View/templates/partials/scoped-partial.blade.php b/tests/Integration/View/templates/partials/scoped-partial.blade.php new file mode 100644 index 000000000000..ab12730fadf6 --- /dev/null +++ b/tests/Integration/View/templates/partials/scoped-partial.blade.php @@ -0,0 +1 @@ +Parent: {{ $parentVar ?? 'undefined' }}, Explicit: {{ $explicitVar }} \ No newline at end of file diff --git a/tests/Integration/View/templates/uses-include-regular.blade.php b/tests/Integration/View/templates/uses-include-regular.blade.php new file mode 100644 index 000000000000..f366d2e237c5 --- /dev/null +++ b/tests/Integration/View/templates/uses-include-regular.blade.php @@ -0,0 +1 @@ +@include('partials.scoped-partial', ['explicitVar' => $explicitVar]) \ No newline at end of file diff --git a/tests/Integration/View/templates/uses-include-scoped.blade.php b/tests/Integration/View/templates/uses-include-scoped.blade.php new file mode 100644 index 000000000000..5e26517cd2cc --- /dev/null +++ b/tests/Integration/View/templates/uses-include-scoped.blade.php @@ -0,0 +1 @@ +@includeIsolated('partials.scoped-partial', ['explicitVar' => $explicitVar]) diff --git a/tests/View/Blade/BladeIncludesTest.php b/tests/View/Blade/BladeIncludesTest.php index 6c96c3d54001..dc65f4dd91b8 100644 --- a/tests/View/Blade/BladeIncludesTest.php +++ b/tests/View/Blade/BladeIncludesTest.php @@ -46,4 +46,12 @@ public function testIncludeFirstsAreCompiled() $this->assertSame('first(["issue", "#45424)"], ["foo" => "bar(-(("], array_diff_key(get_defined_vars(), [\'__data\' => 1, \'__path\' => 1]))->render(); ?>', $this->compiler->compileString('@includeFirst(["issue", "#45424)"], ["foo" => "bar(-(("])')); $this->assertSame('first(["issue", "#45424)"], [(string) "foo()" => "bar(-(("], array_diff_key(get_defined_vars(), [\'__data\' => 1, \'__path\' => 1]))->render(); ?>', $this->compiler->compileString('@includeFirst(["issue", "#45424)"], [(string) "foo()" => "bar(-(("])')); } + + public function testIncludeScopedsAreCompiled() + { + $this->assertSame('make(\'foo\')->render(); ?>', $this->compiler->compileString('@includeIsolated(\'foo\')')); + $this->assertSame('make(\'foo\', [\'((\'])->render(); ?>', $this->compiler->compileString('@includeIsolated(\'foo\', [\'((\'])')); + $this->assertSame('make(\'foo\', [\'((a)\' => \'((a)\'])->render(); ?>', $this->compiler->compileString('@includeIsolated(\'foo\', [\'((a)\' => \'((a)\'])')); + $this->assertSame('make(name(foo))->render(); ?>', $this->compiler->compileString('@includeIsolated(name(foo))')); + } }