Different headers in different views (Blade template, Laravel) - php

I use the Bladetemplate of Laravel. Is there a way to set different header for different views with only one include in master.blade.php?
master.blade.php
#include("elements.header")
#yield('content')
#section("footer")
#show
view.blade.php
#extends("layouts.master")
#section("title")
#stop
#section("content")
#include("elements.error")
#section("footer")
#include("elements.footer")
#stop

If you want to include different header templates for different views, there is no need to include anything from your layout. Instead, include the proper header template into a separate section in your views and then display that section in the master template:
master.blade.php
#yield('header')
#yield('content')
viewA.blade.php
#extends("layouts.master")
#section('header')
#include('headerA')
#stop
#section('content')
view content
#stop
viewB.blade.php
#extends("layouts.master")
#section('header')
#include('headerB')
#stop
#section('content')
view content
#stop
This way, each of your views includes different header templates into the header section that will be later displayed in master layout with #yield('header').

In your main layout
<title>#yield('title','Home')</title>
Then In your views Just call
#section('title','My View 1')
#section('title','My View 2')
The second parameter in yield is the default if none defined.

Related

What is the difference between Laravel #extends and #include

I'am new to Laravel I just want to know the difference between #extends and #include
#extends('tempalate')
can I use #include to add template file in my laravel project.
#include('tempalate')
To simply put it:
Using #include('') function you are including or adding an existing file.
Using #extends('') your are sending a portion of your file to the extended file.
Which is usually wrapped inside a #section('') function.
As per Laravel Documentation:
Blade's #include directive allows you to include a Blade view from
within another view. All variables that are available to the parent
view will be made available to the included view:
<div>
#include('shared.errors')
<form>
<!-- Form Contents -->
</form>
</div>
When defining a child view, use the Blade #extends directive to
specify which layout the child view should "inherit". Views which
extend a Blade layout may inject content into the layout's sections
using #section directives. Remember, as seen in the example above, the
contents of these sections will be displayed in the layout using
#yield:
<!-- Stored in resources/views/child.blade.php -->
#extends('layouts.app')
#section('title', 'Page Title')
#section('sidebar')
#parent
<p>This is appended to the master sidebar.</p>
#endsection
#section('content')
<p>This is my body content.</p>
#endsection
Laravel -> Blade Templates -> Including Subviews

Laravel Blade: #stop VS #show VS #endsection VS #append

In Laravel, there are different ways of using sections:
#yield('section1') // Filled in by child view
#section('section2')
// Default content, can be overwritten by child
// Different ways of closing the section
#endsection|stop|show|append
Who can tell me what the exact difference is between all of these?
Accoding to this, #stop and #endsection might be the same. (with one having been deprecated, but not anymore)
#endsection and #stop are the same and indicate the end of a section.
The section is not actually rendered on the page until you do #yield('sectionname')
In contrast, #show is equivalent to
#stop
#yield('sectionname')
i.e. it stops and immediately renders the section at that part of the page.
#append is basically equivalent to:
//FileA.blade.php
#section('sectionname')
... content
#stop
//FileB.blade.php
#extends('fileA')
#section('sectionname')
#parent
... more content after content
#stop
Here's some relevant source code:
protected function compileStop() {
return '<?php $__env->stopSection(); ?>';
}
protected function compileEndsection() {
return '<?php $__env->stopSection(); ?>'; //Same code
}
protected function compileShow() {
return '<?php echo $__env->yieldSection(); ?>';
}
Yield section just stops the current section and yields its contents.
I might be late in the party. But in Laravel 7.x series, there is no mention of "#stop" and "#append".
Q: Difference between #endsection and #show
#endsection directive just tells the blade engine where the section actually ends. And to show that section you need to use the #yield directive. If you don't yield that section blade won't show it after rendering the view.
For example in layout view:
<!-- layout -->
<body>
#section('content')
Some Content
#endsection
</body>
The above code has no meaning, of course we have defined a section. But it won't show up in the view to the client. So we need to yield it, to make it visible on the client. So lets yield it in the same layout.
<!--layout-->
<body>
#section('content')
Some content
#endsection
#yield('content')
</body>
Now the above code has some meaning to the client, because we have defined a section and told the Blade engine to yield it in the same layout.
But Laravel provides a shortcut to that, instead of explicitly yielding that section in the same layout, use the #section - #show directive pairs. Therefore the above code can be written as follows:
<!--layout-->
<body>
#section('content')
Some content
#show
</body>
So #show is just a compacted version of #endsection and #yield directives.
I hope I made everything clear. And one more thing, in laravel 7.x to append the content in the section, #parent directive is used.
Found it in quora
#show has similar functionality like #yield but you can use it for more things. When you declare #yield in your layout view you can set default value as well.
app.blade.php
#yield('title', 'Default Title')
and in your child view you can change the title
user.blade.php
#section('title', 'Custom Title')
However, you can set only string for default value in case of #yield. If you want to use some part of html content as a default part, you should use #show
app.blade.php
#section('some_div')
<h1>Default Heading1</h1>
<p>Default Paragraph
<span>Default Span</span>
</p>
#show
in your child view
#section('some_div')
<h1>Custom Heading1</h1>
<p>Custom Paragraph
<span>Custom Span</span>
</p>
#endsection
also you can use both default content and custom content together. just include #parent
#section('some_div')
#parent
<h1>Custom Heading1</h1>
<p>Custom Paragraph
<span>Custom Span</span>
</p>
#endsection

How to using extending the same masterpage layout in two partials?

I have two page on Laravel.
I can easily extend this layout in another blade partial to get a working modal with a header, content, and footer which I can embed using #extends('master').
My problem is:
The first page is using header1.blade.php.
And second page is using header2.blade.php.
On master.blade.php is my master page.
<body>
#include('partials.header1')
#yield('content')
#include('partials.footer')
</body>
On index.blade.php is using master.blade.php is master page with extends('master.blade.php).
On listnews.blade.php have same master page.
I want at listnews.blade.php using partials.header2.
Have any way to do this?
As an alternative to #lewis4u's answer you can use the #section directve.
This way you can define the default header to be used but you can change it whenever you need to.
Firstly, change your master.blade.php file to be:
<body>
#section('header')
#include('partials.header1')
#show
#yield('content')
#include('partials.footer')
</body>
Then in your listnews.blade.php file just add another section after the extends:
#section('header')
#include('partials.header2')
#endsection
Hope this helps!

Laravel - How to load content with new URL

I have inherited a Laravel project to which I need to add a new page with some functionality that I have created. What I've got appears to be a main "app.blade.php" file, which includes some stuff that will always be visible.. like sidebar, login auth stuff and so on.
Now adding stuff to this is no problem. But what I want is a separate .php file that is loaded in the main content area of the app.blade.php when I go to a certain URL, let's call it "mypage.com/newpage". (Essentially, I want a link in the sidebar to load this new content.)
So my custom content should appear in the main content area, but the standard sidebar, etc, should still be there. I'm guessing it's something with routes, but... How do I proceed? Which files do I edit? What do I add and where? I already got my new HTML and Javascript code ready - I simply need to add it into the Laravel project the right way.
Suppose , bellow code is your app.blade.php file which you want to inherit.
<html>
<head>
<title>App Name - #yield('title')</title>
</head>
<body>
#section('sidebar')
This is the master sidebar.
#show
<div class="container">
#yield('content')
</div>
</body>
</html>
and you want to load the app.blade.php file here. You need to extends the page and declare the sections like this.
#extends('app')
#section('title', 'Page Title')
#section('sidebar')
#parent
<p>This is appended to the master sidebar.</p>
#endsection
#section('content')
<p>This is my body content.</p>
#endsection
Extending a layout on laravel This may help you.
What you're looking for is template inheritance. You create a layout template that you use as the main layout and your pages inherit that main layout template. See the Laravel Blade documentation: https://laravel.com/docs/5.2/blade#template-inheritance

load js when loading a subview in laravel

Is there a way to load js into the <head> section of the page whena subview is loaded. I would like to only load specific JS files based on the view that is being called. I am using the blade template engine.
There is a much easier way to do this using sections and template inheritance.
Firstly, create a master layout file, something like this:
http://paste.laravel.com/UY9 It's one I use that includes
Initializr/Bootstrap. I store this in views/layouts/frontend/ as
master.blade.php for the frontend and views/layouts/admin/ as
master.blade.php for the admin and amend as necessary.
You'll notice various #section declarations ending with #show. Using
#show at the end instead of #stop, allows you to override them in
your other views, you'll notice I've added a #section('scripts')
declaration. So, you can create a view like so:
#extends('layouts.frontend.master')
#section('scripts')
Your Scripts
#stop
#section('content')
Your content
#stop
It's that simple. This is very powerful stuff, as it gives you the ability to set a default but also override it if necessary, keeping your views very clean and minimal.
A better way to do this though would be to do:
#extends('layouts.frontend.master')
#section('head')
#parent
Your Scripts
#stop
#section('content')
Your content
#stop
Then you can remove the #section('scripts') declaration from your master layout. Using the #parent helper will allow you to append content to a section, thus keeping it's default while adding the extra content you have specified in your view.
You can also provide default content for #yield declarations, like so #yield('content', 'Default content').
Check out http://codebright.daylerees.com/blade
First make a common header layout.
app/views/layouts/header.blade.php - header layout
<!DOCTYPE html>
<html>
<head>
<title>{{ $page->title }}</title> {{-- Getting title from $page object, which is passed from route --}}
{{ HTML::style('css/common.css') }}
{{ HTML::script('js/jquery.js') }}
{{ HTML::script('js/common.js') }}
Then page specific script layout.
app/views/layouts/script-about.blade.php - about-page script layout
{{ HTML::script('js/about.js') }}
Then view for specific page.
app/views/about.blade.php - about page
#extends('layouts.master')
#section('content')
<p>About-Us page content goes here</p>
#stop
Then common footer.
app/views/layouts/footer.blade.php - footer layout
</body>
</html>
Then the main layout to render.
app/views/layouts/master.blade.php - main layout
#include('layouts.header')
#include('layouts.script-'.$page->slug) {{-- Getting page name from $page object --}}
</head>
<body>
#yield('content')
#include('layouts.footer')
And from the route, you can pass the $page variable. You may like this route,
Route::get('{slug}', function($slug) {
$page = Page::where('slug', '=', $slug)->first();
// getting the datas from Page model with pages table where slug = last uri in your address-bar
if ( is_null($page) ) { // if no page in db, call 404
App::abort(404);
}
// render the view with page details
return View::make($page->slug)->with(array('page' => $page));
});
In laravel 5.4 as stated in the document
You can simply use stacks(#stack and #push) to be able to load CSS and JS from subviews(child views).
Add #stack to your layout where you want JS files or CSS files to be added from child views to layout.
Here I will define two stacks in the layout file one for CSS files and one for JS files. I give the first and the second stacks arbitrary name for instance styles and scripts
we want our CSS files to be loaded in the part of layout file
<head>
#stack('styles')
</head>
and let's say we want our scripts to be added right be for the closing body tag
in layout file
#stack('scripts')
</body>
now in the child view i can easily add CSS and JS files like this
#push('styles')
<style rel="stylesheet" href="{{asset('dropzone/dist/min/dropzone.min.css') }}" ></style>
#endpush
#push('scripts')
<script src="{{ asset('dropzone/dist/min/dropzone.min.js') }}"></script>
#endpush

Categories