Skip to content

Conversation

@KennedyTedesco
Copy link
Contributor

@KennedyTedesco KennedyTedesco commented Jan 7, 2026

This PR introduces a new @includeIsolated Blade directive that includes a view without inheriting the parent scope's variables.

I work in a codebase where we use <x-component> for reusable components and @component() for partials (because we like variable isolation). However, @component() brings overhead we don't need: slot management, the opening/closing tag ceremony, and component lifecycle logic.

@includeIsolated would give us the best of both worlds: the simplicity of @include with the isolation of @component.

This PR is another shot of: #45616

Motivation

Currently, @include passes all parent scope variables via get_defined_vars():

{{-- Parent view has $user, $errors, $config, etc. --}}

@include('partials.hero', ['message' => $error])
{{-- hero.blade.php receives $message + ALL parent variables --}}

This implicit inheritance can lead to:

  • Variable collisions in nested partials
  • Unpredictable behavior when partials accidentally depend on the parent scope
  • Harder debugging when variables "magically" appear

The recommended workaround is Blade components (<x-partials.hero />), but components come with overhead
(slots, validation, class resolution) that isn't always needed for simple partials.

Proposed Solution

@includeIsolated() provides explicit-only data passing where "explicit > implicit":

@includeIsolated('partials.hero', ['message' => $error])
{{-- hero.blade.php receives ONLY $message --}}

Benefits:

  • Cleaner syntax for isolated partials (no closing tag needed)
  • Lighter than @component (no slot/component machinery)
  • Consistent with existing @includeIf, @includeWhen, @includeFirst patterns
  • Prevents accidental variable leakage and naming collisions

@taylorotwell taylorotwell merged commit 5162099 into laravel:12.x Jan 8, 2026
70 checks passed
@KennedyTedesco KennedyTedesco changed the title [12.x] Add @includeScoped directive for isolated Blade includes [12.x] Add @includeIsolated directive for isolated Blade includes Jan 8, 2026
@alies-dev
Copy link
Contributor

this is great, @KennedyTedesco!!! Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants