Search code examples
laravellaravel-blade

Analogy Laravel [@extends @yield] and [export import] of JS


My undestanding of layouts in Laravel is:
main.blade.php will import a partial code with @yield:

<!-- main.blade.php -->
<html>
    <head>
        <title>@yield('title')</title>
    </head>
    <body>
        @yield('content')
    </body>
</html>

And the partial code will export itself to a specific file:

<!-- partial.blade.php -->
@extends('layouts.main')

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

@section('content')
    <p>This is the content of the page.</p>
@endsection

If I understand correctly, it's the partial itself that decides where to go?
If so, big is my headache... I don't understand the logic.
What happens if several views want to use the same partial? Why isn't it the other way round ( @yield('layouts.partial','title') ), like in JS, where the one importing the partial indicates its path?


Solution

  • I think you have the wrong idea. Partials or components don't extend a layout. They are included as is via @component('components.modal') or @include('partials.paginate').

    What you have here:

    <!-- partial.blade.php -->
    @extends('layouts.main')
    
    @section('title', 'Page Title')
    
    @section('content')
        <p>This is the content of the page.</p>
    @endsection
    

    is not a partial.

    This is a page you serve via return view('path.to.view'); The layout is not the page, otherwise you would be serving the layout. This page now can define, but it does not have to, what layout it extends and what parts of that layout it overwrites, title and content.