Laravel Blade Section Example

10-Apr-2023

.

Admin

Hi Guys,

In this blog, I will show you laravel blade section example. We will talk about blade section example in laravel appliation.

Layouts may also be created via "template inheritance". This was the primary way of building applications prior to the introduction of components.

To get started, let's take a look at a simple example. First, we will examine a page layout. Since most web applications maintain the same general layout across various pages, it's convenient to define this layout as a single Blade view:

As you can see, this file contains typical HTML mark-up. However, take note of the @section and @yield directives. The @section directive, as the name implies, defines a section of content, while the @yield directive is used to display the contents of a given section.

Defining A Layout


resources/views/layouts/app.blade.php

<html>

<head>

<title>App Name - @yield('title')</title>

</head>

<body>

@yield('sidebar')

<div class="container">

@yield('content')

</div>

</body>

</html>

Now that we have defined a layout for our application, let's define a child page that inherits the layout.

Extending A Layout

When defining a child view, use the @extends Blade directive to specify which layout the child view should "inherit". Views which extend a Blade layout may inject content into the layout's sections using @section directives. Remember, as seen in the example above, the contents of these sections will be displayed in the layout using @yield:

resources/views/child.blade.php

@extends('layouts.app')

@section('title', 'Page Title')

@section('sidebar')

<p>This is appended to the master sidebar.</p>

@endsection

@section('content')

<p>This is my body content.</p>

@endsection

It will help you....

#Laravel 8

#Laravel 7

#Laravel

#Laravel 6