V
V
Valery Khizhevsky2017-07-21 18:24:00
Laravel
Valery Khizhevsky, 2017-07-21 18:24:00

How does @yield work in Laravel?

How @yield works
3a27d5ae0a4f43b0b8e80d8f51658343.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
D3lphi, 2017-07-21
@Roshette

You create a template (Let's call it first.blade.php), insert @yield() into it:

<div class="example">
    @yield('content')
</div>

Then you inherit from this template another template (second.blade.php) and write the @section() construct to it with the same name as you specified for yield:
@extends('first')

@section('content')
    Hello, World!
@endsection

Now you render the second template:
class Controller
{
    public function indexAction()
    {
        return view('second');
    }
}

The following page is rendered:
<div class="example">
    Hello, World!
</div>

Thus, it turns out that @yield() serves as a kind of marker, in place of which the contents of @section() of the child template will be substituted.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question