Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Illuminate/View/Compilers/Concerns/CompilesIncludes.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,17 @@ protected function compileIncludeFirst($expression)

return "<?php echo \$__env->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 "<?php echo \$__env->make({$expression})->render(); ?>";
}
}
19 changes: 19 additions & 0 deletions tests/Integration/View/BladeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Parent: {{ $parentVar ?? 'undefined' }}, Explicit: {{ $explicitVar }}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@include('partials.scoped-partial', ['explicitVar' => $explicitVar])
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@includeIsolated('partials.scoped-partial', ['explicitVar' => $explicitVar])
8 changes: 8 additions & 0 deletions tests/View/Blade/BladeIncludesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,12 @@ public function testIncludeFirstsAreCompiled()
$this->assertSame('<?php echo $__env->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('<?php echo $__env->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('<?php echo $__env->make(\'foo\')->render(); ?>', $this->compiler->compileString('@includeIsolated(\'foo\')'));
$this->assertSame('<?php echo $__env->make(\'foo\', [\'((\'])->render(); ?>', $this->compiler->compileString('@includeIsolated(\'foo\', [\'((\'])'));
$this->assertSame('<?php echo $__env->make(\'foo\', [\'((a)\' => \'((a)\'])->render(); ?>', $this->compiler->compileString('@includeIsolated(\'foo\', [\'((a)\' => \'((a)\'])'));
$this->assertSame('<?php echo $__env->make(name(foo))->render(); ?>', $this->compiler->compileString('@includeIsolated(name(foo))'));
}
}