Skip to content

Commit 5162099

Browse files
[12.x] Add @includeScoped directive for isolated Blade includes (#58311)
* [12.x] Add `@includeScoped` directive for isolated Blade includes * Remove empty array from includeScoped compilation * formatting --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent bdc94ce commit 5162099

File tree

6 files changed

+43
-0
lines changed

6 files changed

+43
-0
lines changed

src/Illuminate/View/Compilers/Concerns/CompilesIncludes.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,17 @@ protected function compileIncludeFirst($expression)
7979

8080
return "<?php echo \$__env->first({$expression}, array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>";
8181
}
82+
83+
/**
84+
* Compile the include-isolated statements into valid PHP.
85+
*
86+
* @param string $expression
87+
* @return string
88+
*/
89+
protected function compileIncludeIsolated($expression)
90+
{
91+
$expression = $this->stripParentheses($expression);
92+
93+
return "<?php echo \$__env->make({$expression})->render(); ?>";
94+
}
8295
}

tests/Integration/View/BladeTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,25 @@ public function testViewCacheCommandHandlesConfiguredBladeExtensions()
225225
$this->assertTrue($found);
226226
}
227227

228+
public function test_include_scoped_does_not_inherit_parent_scope()
229+
{
230+
// Regular @include passes parent scope variables
231+
$regularInclude = View::make('uses-include-regular', [
232+
'parentVar' => 'parent-value',
233+
'explicitVar' => 'explicit-value',
234+
])->render();
235+
236+
$this->assertSame('Parent: parent-value, Explicit: explicit-value', trim($regularInclude));
237+
238+
// @includeIsolated does NOT pass parent scope variables
239+
$scopedInclude = View::make('uses-include-scoped', [
240+
'parentVar' => 'parent-value',
241+
'explicitVar' => 'explicit-value',
242+
])->render();
243+
244+
$this->assertSame('Parent: undefined, Explicit: explicit-value', trim($scopedInclude));
245+
}
246+
228247
/** {@inheritdoc} */
229248
#[\Override]
230249
protected function defineEnvironment($app)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Parent: {{ $parentVar ?? 'undefined' }}, Explicit: {{ $explicitVar }}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@include('partials.scoped-partial', ['explicitVar' => $explicitVar])
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@includeIsolated('partials.scoped-partial', ['explicitVar' => $explicitVar])

tests/View/Blade/BladeIncludesTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,12 @@ public function testIncludeFirstsAreCompiled()
4646
$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(-(("])'));
4747
$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(-(("])'));
4848
}
49+
50+
public function testIncludeScopedsAreCompiled()
51+
{
52+
$this->assertSame('<?php echo $__env->make(\'foo\')->render(); ?>', $this->compiler->compileString('@includeIsolated(\'foo\')'));
53+
$this->assertSame('<?php echo $__env->make(\'foo\', [\'((\'])->render(); ?>', $this->compiler->compileString('@includeIsolated(\'foo\', [\'((\'])'));
54+
$this->assertSame('<?php echo $__env->make(\'foo\', [\'((a)\' => \'((a)\'])->render(); ?>', $this->compiler->compileString('@includeIsolated(\'foo\', [\'((a)\' => \'((a)\'])'));
55+
$this->assertSame('<?php echo $__env->make(name(foo))->render(); ?>', $this->compiler->compileString('@includeIsolated(name(foo))'));
56+
}
4957
}

0 commit comments

Comments
 (0)