Pregunta

I'll follow some tutorials about Laravel 3, and know, I have a problem, with one:

@section('post_navigation')
@if(Auth::check())
    @include('plugins.loggedin_postnav')
@endif
@endsection

Why this section not appear?

I try remove all content in section, for example;

@section('post_navigation')
    <h1>Test</h1>
@endsection

But doesn't work.

The complete code is that:

<div class="navbar navbar-inverse navbar-fixed-top">
    <div class="navbar-inner">
        <div class="container">
            {{ HTML::link('/', 'Instapics', array('class' => 'brand')); }}

            <div class="nav-collapse">
                <ul class="nav">
                    @section('navigation')
                        <li class="active">{{ HTML::link('/', 'Home') }}</li>
                    @yield_section
                </ul>
            </div>

            @section('post_navigation')
            @if(Auth::check())
                @include('plugins.loggedin_postnav')
            @endif
            @endsection

        </div>
    </div>
</div>


[EDIT]
I change @endsection to @yield_section, and works, BUT, I still not understand, for example (in User_Controller index view):

@section('post_navigation')
    @parent
@endsection

Why not appear the include?

Why I need change endsection to yield_section?

¿Fue útil?

Solución

When you use @section, you define a section like defining a variable. Then, you can use it where you want by @yield('sectionname') (or by using another way I specified in the second paragraph). For example, you can look at this:

@section('scripts')
    <script src="jquery.js"></script>
@endsection

<head>
    @yield('scripts')
</head>

@section / @endsection and @section / @yield_section are not same. @section / @yield_section defines a section area, not a section. In other words, it calls a variable. Actually it is more similar to yield('sectionname') than @section / @endsection. It has default value as a main difference from yield.

You can define an area which has default value, and then you can change it by defining a section. This logic mostly used while creating and using layouts. For example, Let below page be our main (main.blade.php) layout.

<html>
    <head>
        <title>
        @section('title')
        Default Title Maybe Sitename.com
        @yield_section
        </title>
        @include('scripts')
    </head>
    <body>
        <div id="content">
        @section('content')
            Default content
        @yield_section
        </div>
        <div id="sidebar">
        @section('sidebar')
            <ul id="menu">
                <li><a href="#">Menu item 1</a></li>
                <li><a href="#">Menu item 2</a></li>
                <li><a href="#">Menu item 3</a></li>
                <li><a href="#">Menu item 4</a></li>
            </ul>
        @yield_section
    </body>
</html>

Generally layouts are not used directly. Another page is created and specified in the page its layout like @layout('main'). Then, sections are defined and laravel templating system change the defined section. Let this page be our post (post.blade.php) page:

@layout('main')

@section('title')
$post->title - @parent
@end_section

@section('content')
$post->content
@end_section

When we return post view View::make('profile');, as you can see, layout logic will work and sections in main.blade.php will be changed with we defined in this page. So, the output will be:

<html>
    <head>
        <title>
        Post title - Default Title Maybe Sitename.com
        </title>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    </head>
    <body>
        <div id="content">
            Post content
        </div>
        <div id="sidebar">
            <ul id="menu">
                <li><a href="#">Menu item 1</a></li>
                <li><a href="#">Menu item 2</a></li>
                <li><a href="#">Menu item 3</a></li>
                <li><a href="#">Menu item 4</a></li>
            </ul>
    </body>
</html>

By the way, @parent returns default value in section area and @include is same logic with include in php.

Have a nice day.

Otros consejos

Seems like you may have found an answer. But it looks like @endsection has been replaced with @stop in L4. Try something like this instead:

@section('scripts')
    <script src="jquery.js"></script>
@stop
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top