Support the ongoing development of Laravel.io →
Views Blade
Last updated 1 year ago.
0

Never thought about it like that actually, very useful :) Thanks for sharing!

Last updated 1 year ago.
0

I'd like to add my thoughts on this as I do similar thing but with bit more flexible way, and you will also find this is useful when it comes to web sites with many pages of different context.

I use @yield only to output content dynamically, more specifically for the content which always change, and if we know something is used all the time or majority of time I use include. I'll explain it in more detail below.

Another reason for my structure is to totally main javascripts and css references into different files, so that I can easily find them and my master layout page only contain the contents. I can also reuse css and scripts in different master files as well. Then we have scripts/css common to all (ex: bootstrap) and there are some specific to pages . by following way we can use them properly only when needed.

master.blade.php (This is the layout file inside view/layout)

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>My Web Site</title>

    @include('layout.style')

  </head>
  <body>
    <div class="container" id="main">

          @yield('content')
    </div><!-- #main -->
    

    @include('layout.script')

  </body>
</html>

as you can see we have included two files one for css and one for scripts

style.blade.php (where we define common .css)

{{ HTML::style('packages/bs/css/bootstrap.min.css') }}

{{ HTML::style('css/style.css') }}

@yield('styles')

Note that we have defined the common css files there but also given option to include any new css files by child pages if required on their own pages by defining a section called 'styles'

script.blade.php (main javascript file page)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min...;></script>

{{ HTML::script('packages/bs/js/bootstrap.min.js') }}

@yield('scripts')

similar to css here we have defined 'scripts' section so sub views can define their own section.

(child view)

@extends('layout.master')

@section('styles')
{{ HTML::style('css/style2.css') }}
@stop


@section('scripts')
{{ HTML::script('js/app.js') }}
@stop


@section('content')
<div class="col-12">
    This is the content
</div>
@stop

As you can see we have included new styles and js which required only in this child view here, so that they won't appear all the time as when we define all scripts in master page.

P.S. As I remember Laravel 3 had tag which we can use to control where should parent view section appear from our child view. I'm not sure they still support it. If it is then we can have more control over precedence of scripts and styles.

Last updated 1 year ago.
0

The OP is actually what I believed to be the intended use of sections and yields and is how I've always used them. technet's post is very clever, and a really neat way to manage page specific scripts and styles, love it.

Last updated 1 year ago.
0

How do you not include a section from the default page in my code the page header has a navbar I do not include in another page but am wondering how not to include it in my other pages .

0

This is a game changer. Thank you so much. Jose-L.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.