0
votes

I don't know what does putting a section inside another section do. In a Laravel project I'm reading, a Blade file's code is like this:
login.blade.php :

@extends('layout') @section('content')
@section('title', 'Log in')
Lots of content.
@endsection

In above, what's the point of having title section inside content section? How will it be different if title section is placed outside:

@extends('layout')
@section('title', 'Log in')
@section('content')
Lots of content.
@endsection

I tested, and both are producing same output (HTML source code).

layout.blade.php

<head><title>@yield('title')</title></head>
<body>@yield('content')</body>

Is there any case of layout.blade.php in which different outputs will be produced?

2
The section directive simply copies whatever you have within @section and @endsection to the name @yield place holder on the extended template. This means it does not consider where its placed, but for clarity and readability, the second example is the right structure - Emeka Mbah

2 Answers

1
votes

The section directive simply copies whatever you have within @section and @endsection to the name @yield place holder on the extended template.

This means it does not consider where its placed, but for clarity and readability, the second example is the right structure.

For example, this shows that the order of the directive doesn't matter

In welcome.blade.php

@extends('default')

@section('a')

    This is a

    @section('c', 'This is c')

    @section('b')
        This is b
        @section('d', 'This is d')
    @endsection

@endsection

In default.blade.php

@yield('c')

@yield('a')

@yield('b')

@yield('d')

The output

This is c This is a This is b This is d

The best practice is to make your code more readable by opening and closing each block:

@extends('default')

@section('a')
    This is a
@endsection

@section('c', 'This is c')

@section('b')
    This is b
@endsection

@section('d', 'This is d')
0
votes

It's better to put them on different rows for readability sake.

First section just puts "Log in" wherever the section "title" is:

@section('title', 'Log in')

Second section has the beginning and the end and is selfdescriptive.