{"id":651,"date":"2023-11-26T09:11:43","date_gmt":"2023-11-26T09:11:43","guid":{"rendered":"https:\/\/www.devopssupport.in\/blog\/?p=651"},"modified":"2023-12-09T06:11:20","modified_gmt":"2023-12-09T06:11:20","slug":"what-is-use-of-yield-extend-section-in-laravel","status":"publish","type":"post","link":"https:\/\/www.devopssupport.in\/blog\/what-is-use-of-yield-extend-section-in-laravel\/","title":{"rendered":"What is use of  @yield @extend @section in Laravel"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"608\" height=\"173\" src=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/06\/image-25.png\" alt=\"\" class=\"wp-image-652\" srcset=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/06\/image-25.png 608w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/06\/image-25-300x85.png 300w\" sizes=\"auto, (max-width: 608px) 100vw, 608px\" \/><\/figure>\n\n\n\n<p>In Laravel, the concepts of layouts, yield, extend, and sections are used to create reusable templates and organize the structure of your application&#8217;s views. These features make it easier to manage the common elements shared across multiple pages while allowing flexibility to customize specific sections of each page.<\/p>\n\n\n\n<p><strong>Creating a Layout:<\/strong><\/p>\n\n\n\n<p>First, you&#8217;ll create a layout file that defines the common structure and components of your application. This file typically resides in the <code>views\/layouts<\/code> directory. Let&#8217;s call it <code>app.blade.php<\/code>.<\/p>\n\n\n\n<p>Here&#8217;s an example of a basic layout file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html>\r\n&lt;html>\r\n&lt;head>\r\n    &lt;title>@yield('title')&lt;\/title>\r\n&lt;\/head>\r\n&lt;body>\r\n    &lt;header>\r\n        &lt;!-- Common header content goes here -->\r\n    &lt;\/header>\r\n\r\n    &lt;main>\r\n        @yield('content')\r\n    &lt;\/main>\r\n\r\n    &lt;footer>\r\n        &lt;!-- Common footer content goes here -->\r\n    &lt;\/footer>\r\n&lt;\/body>\r\n&lt;\/html>\r\n<\/code><\/pre>\n\n\n\n<p>In this layout, you can see that we have defined sections for the title, content, header, and footer. The <code>@yield<\/code> directives indicate the areas where content from other views can be injected.<\/p>\n\n\n\n<p><strong>Extending the Layout:<\/strong><\/p>\n\n\n\n<p>To utilize the layout, you need to create views that extend it. These views will inherit the structure and components defined in the layout while allowing you to customize specific sections. Let&#8217;s create a view called <code>home.blade.php<\/code> as an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@extends('layouts.app')\r\n\r\n@section('title', 'Home')\r\n\r\n@section('content')\r\n    &lt;h1>Welcome to our website!&lt;\/h1>\r\n    &lt;p>This is the content of the home page.&lt;\/p>\r\n@endsection\r\n<\/code><\/pre>\n\n\n\n<p>In this view, we use the <code>@extends<\/code> directive to specify that we want to extend the <code>app.blade.php<\/code> layout. We also use the <code>@section<\/code> directive to define the content for the <code>title<\/code> and <code>content<\/code> sections.<\/p>\n\n\n\n<p><strong>Rendering the View:<\/strong><\/p>\n\n\n\n<p>To render the final view, you can use the <code>view<\/code> helper function in your routes or controllers. Here&#8217;s an example of how you can render the <code>home.blade.php<\/code> view:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Route::get('\/', function () {\r\n    return view('home');\r\n});\r\n<\/code><\/pre>\n\n\n\n<p>When the <code>home.blade.php<\/code> view is rendered, Laravel will automatically pull in the content defined in the <code>@section<\/code> directives and inject them into the corresponding <code>@yield<\/code> directives in the layout. This allows the final output to include the common layout structure along with the customized content specific to the home page. By using layouts, yield, extend, and sections, you can create a consistent structure for your views while providing the flexibility to customize individual sections. This approach simplifies the management of shared elements and improves the maintainability of your Laravel application&#8217;s views.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Laravel, the concepts of layouts, yield, extend, and sections are used to create reusable templates and organize the structure of your application&#8217;s views. These features make&#8230; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-651","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/651","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/comments?post=651"}],"version-history":[{"count":1,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/651\/revisions"}],"predecessor-version":[{"id":653,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/651\/revisions\/653"}],"wp:attachment":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/media?parent=651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/categories?post=651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/tags?post=651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}