F
F
Flasher2017-05-02 16:06:48
Laravel
Flasher, 2017-05-02 16:06:48

How to properly share data via @section, @yield in blade?

I have main layouts,app :

<head>
    <meta charset="utf-8">
</head>
<body>
        @yield('news')

        @yield('other_news')
</body>
</html>

In the controller, I connect the view and in the view, something like this @yield('news') :
@extends('layouts.app')
@section('news')
тут вывод новости
@endsection

But how can I make it so that on the internal news view page, I can display content with @yield('other_news') ? After all, in the controller I connect another view, in which it also lies, something like:
@extends('layouts.app')
@section('news')
Другие новости
@endsection

It turns out that this part of @yield('other_news') is not output.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
F
Flasher, 2017-05-02
@Flasher

Thanks for the help @den-masonov
This is what the templates should look like:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>

@yield('content')
@yield('comment')
@yield('news')

</body>
</html>

@extends('layouts.app')

@section('content')
    {{ $content = 'Контент' }} <br>
@endsection


@section('comment')
    {{ $comment = 'Коммент' }} <br>
@endsection


@section('news')
    {{ $news = 'Новости' }} <br>
@endsection

@extends('news.index')

@section('content')
    {{ $news = 'Вложенные новости' }} <br><br>
@endsection

O
Oleg Timoshenko, 2017-05-02
@gultaj

Multiple sections can be displayed

@extends('layouts.app')

@section('news')
    Другие новости
@endsection

@section('other_news')
    Ещё новости
@endsection

F
Finsh, 2017-05-02
@Finsh

In the body you are displaying a cell of content that will be available to all pages. You do not need to add labels for all pages
. To access other_news, do the same as with news, but you should not do this. Usually this cell is called content and is used for all pages

@extends('layouts.app')
@section('news')
Новости
@endsection
@section('other_news')
Другие новости
@endsection

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question