What is the difference between Section and Stack in Blade?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
# Section vs. Stack in Blade: Mastering Content Inheritance in Laravel As developers working with Laravel and Blade templating, we constantly seek ways to write DRY (Don't Repeat Yourself) code. Blade offers powerful mechanisms for reusing HTML structures, but when it comes to structuring reusable components across different views, the choice between using `@section`/`@yield` and `@stack`/`@push` is crucial. While both serve the purpose of content injection, they operate on fundamentally different principles. Understanding this distinction is key to writing flexible, maintainable, and scalable Blade templates. ## The Inheritance Model: `@section` and `@yield` The `@section` and `@yield` directives implement a classic **inheritance model**. This pattern is ideal when you have a clear, hierarchical relationship between parent views and child views—for example, a main layout file defining the overall structure (like ``, ``), and child components providing specific content for those defined slots. In this system: 1. **`@section('name')`**: Defines a specific block of content available for that particular view segment. 2. **`@yield('name')`**: Acts as the placeholder where the content from the parent view (or another section) will be injected. This mechanism enforces a one-to-one mapping. If you define a slot, you expect exactly one piece of content to populate it. This is perfect for defining static layout elements like headers or footers. ### Example: Using Sections for Layout Consider setting up a basic page structure where the main layout provides the scaffolding. ```blade {{-- resources/views/layout.blade.php (The Parent View) --}} My Application @stack('head_content') {{-- Using stack for flexibility here, see below --}}
Site Header
@yield('content') {{-- This is the main content slot --}}
© 2024
{{-- resources/views/home.blade.php (The Child View) --}} @section('content')

Welcome Home

This is the specific content for the home page.

@endsection ``` ## The Collection Model: `@stack` and `@push` If sections are for defining fixed slots, stacks are for **collecting dynamic, optional content**. The `@stack` directive allows you to define a named collection point. Other views can then use `@push` to inject content into that stack, and the parent view uses `@stack` to render everything collected in that list. This approach is significantly more flexible because it avoids rigid inheritance. It allows multiple, potentially unrelated components to contribute content to a single slot without forcing them into a strict parent-child relationship. ### Example: Using Stacks for Modular Content Using stacks makes the layout much more adaptable, especially when dealing with optional scripts or CSS files. ```blade {{-- resources/views/layout.blade.php (The Parent View) --}} My Application @stack('head_content') {{-- Rendering all content pushed into the 'head_content' stack --}}
Site Header
@yield('content')
© 2024
{{-- resources/views/home.blade.php (The Child View) --}} @push('head_content') Welcome Home @endpush @section('content')

Welcome Home

This is the specific content for the home page.

@endsection ``` ## When to Choose Which? Developer Best Practices The choice between `@section`/`@yield` and `@stack`/`@push` boils down to the structure of your application: 1. **Use `@section`/`@yield` for Hierarchical Layouts:** Use this when you are building a rigid, nested layout where every view modifies a specific part of a known parent structure (e.g., defining page titles or main content blocks). 2. **Use `@stack`/`@push` for Modular Components:** Use stacks when you need to collect arbitrary, optional content that might be added by many different components across the application. This is superior for managing assets like CSS links, JavaScript bundles, or conditionally rendered navigation items, as it decouples the content injection from a strict inheritance chain. By understanding these two patterns, you move beyond simple template inclusion and gain the ability to build truly dynamic and decoupled Blade views. For more in-depth details on advanced Blade features and layout management within Laravel, always refer to the official documentation provided by the team at [laravelcompany.com](https://laravelcompany.com).