Performance: Template inheritance (@extends, @section, @yield)
This affects server-side rendering speed and the size of the HTML sent to the browser, impacting initial page load time.
@extends('layouts.master') @section('content') <div>Page content</div> @endsection
@php echo file_get_contents('header.html'); @endphp <div>Page content</div> @php echo file_get_contents('footer.html'); @endphp
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual HTML includes | No direct DOM impact (server-side) | N/A | Larger HTML increases paint time | [X] Bad |
| Blade template inheritance | No direct DOM impact (server-side) | N/A | Smaller HTML reduces paint time | [OK] Good |
@extends directive in Laravel Blade templates?@extends@extends directive tells Laravel which base layout file the current view should build upon.@section defines content blocks, @yield displays them, and including partials uses @include. Only @extends sets the base layout.@extends = base layout [OK]@extends picks the base layout file [OK]@extends with @section@extends outputs content@extends with @include@section('name') starts a content block, and @endsection ends it.@yield is for displaying sections, @extends sets layout, and @include inserts partials. Only @section defines content.@section('name') [OK]@section('name') to start content blocks [OK]@yield to define content@extends as a section@include with section definitionlayouts/app.blade.php:
<html>
<head>
<title>My Site - @yield('title')</title>
</head>
<body>
@yield('content')
</body>
</html>
And a child view:
@extends('layouts.app')
@section('title', 'Home Page')
@section('content')
<h1>Welcome!</h1>
@endsection
What will be the rendered HTML output?@yield and @section interaction@yield('title') and @yield('content') to display content from child sections.@section('title', 'Home Page') and a content section with an <h1> tag. These replace the yields in the layout.@yield shows @section content [OK]@yield outputs content from matching @section [OK]@section tags in output@section('title', 'text') sets inline content@yield with @section definition@extends('layouts.master')
@section('content')
<p>Hello World</p>
What is the problem with this code?@section('content') directive must be closed with @endsection to mark the end of the content block.@endsection, causing a syntax error or unexpected behavior.@endsection to close the content section -> Option A@section with @endsection [OK]@section with @endsection [OK]@section@yield defines content@yield placeholders: title, header, and content. You want to create a child view that fills only content and uses a default title and header from the layout. How should you write the child view?title and header, you don't need to override those sections in the child view.content section in the child view to fill that placeholder, leaving others to default.content section -> Option B@yield inside child views@includePass the test and this pattern moves unknown