Laravel include an extended partial - laravel

Im trying to include a partial, that extends another partial.
Like #include('B')and B looks like
#extends('A')
#section('content')
Some stuff here
#stop
But it seems that only Some stuff heregets displayed, but not the things of partial A. Do you have any fixes for that?
Thanks

Seems like you're trying to use partial B that extends template A
If you need to append data, you can use the #parent or #append notation, like so:
#extends('A')
#section('content')
#parent
Some stuff here
#stop
This will append parent data from template A's content section, inside template B
More info at Laravel's Blade documentation

Actually this works. The problem here was the non-unique use of the #section Identifier. (In the base Template and in the partial)

Related

laravel blade template - how to remove a part from extended page

Note: Like i said in the comment, i am working relatively large project. I can't change existing codes. Just try to add some code blocks for one page.
I have a template blade. It has meta yield. But also included one meta.blade.php, that contains all meta tags. But i don't want to include metapage for some of my pages. There is the template for visualization:
my_template
<header>
#yield('meta')
#include('metapage')
#yield('style')
#yield('js')
</header>
my view.blade.php
#extends('my_template')
#section('meta')
<meta description...>
#endsection
#section('style')
//content
#endsection
#section('js')
//content
#endsection
My question is: Is there a way to make something like this:
#extends('my_template')->except('metapage')
I know there isn't exist something like that. But i need that. I hope somebody can give me a solution.
There is a certain solution which I do:
First one is to create multiple template and extend them as your requirements.
Second one is to disable sub parts.
Third one to create parent template having little things, then create child template which is extending parent and do extra things here. use it as your need.
If You are working on existing project and you have a lot of pages then First & third one is a better solution for it because you could make changes only in front end without affecting class code.
You could make the meta a component rather than an include, then your template would look the same and your view would be e.g.
#extends('my_template')
#section('meta')
#component('meta')
#slot('description', 'my amazing description')
#endcomponent
#endsection
// other code here as usual
Your component is then responsible for checking what exists and what doesn't, e.g. like this:
#isset($description)
<meta name="description" content="{{ $description }}">
#endisset
#isset($title)
<title>{{ $title }}</title>
#endisset
// etc, the title is just an example
Documentation: https://laravel.com/docs/5.6/blade#components-and-slots
Take the Tag Name Which You Want To ignore And Apply the CSS
ex: If I need to Remove the Footer From the Extended Page make
footer {
display: none;
}

extending laravel blade comming to top

i am an absolute beginner in laravel i have a basic structure for now, there is an app template which contains the structure of the view i.e
head
navbar
#yield("content")
footer
now the structure is working fine now for some specific pages i have to include google map the map blade is placed inside includes/map.blade.php this is my index blade
#extends("app")
#section("content")
the content goes here
#extends("includes.map")
#endsection
basically after the content i want to put the map so that it comes above footer but somehow whenever i try to extent it it always comes at the top
If you want to include the map then it should be #include (doc):
#extends("app")
#section("content")
the content goes here
#include("includes.map")
#endsection
You cannot use two extends.
Instead of #extends("includes.map") use #include("includes.map")

Laravel #push not working in create_category.blade.php

Created a template folder inside views folder and in header.blade.php and added the following code
<ul id="sidebar">
#stack('sidebar')
</ul>
And created another category.blade.php inside views folder and added the following code
#include('template/header')
#push('sidebar')
<li>Sidebar first</li>
#endpush
A #stack can only be defined in an outer layout. So if you want to use #push, you have to use #extends('template.header') instead of #include('template.header').
In your case I don't think it is okay to use #extends you probably use an other layout file.
I searched for it in the Laravel issues and I found this Thread. Maybe this can clarify some things for you.
This can be particularly useful for specifying any JavaScript libraries required by your child views
better to use #include insted #push and #stack

What's the difference between Laravel Blade's `#yield` and `#include`?

I'm learning Laravel (starting at version 5.3) and these two Blade directives look very similar, the only difference I know is that #include injects the parent's variables and can also send other variables.
What's the difference between #yield and #include?
When should I use #yield?
When should I use #include?
#yield is mainly used to define a section in a layout. When that layout is extended with #extends, you can define what goes in that section with the #section directive in your views.
The layout usually contains your HTML, <head>, <body>, <header> and <footer>s. You define an area (#yield) within the layout that your pages which are extending the template will put their content into.
In your master template you define the area. For example:
<body>
#yield('content')
</body>
Let's say your home page extends that layout
#extends('layouts.app')
#section('content')
// home page content here
#endsection
Any HTML you define in the content section on your homepage view in the 'content' section will be injected into the layout it extended in that spot.
#include is used for reusable HTML just like a standard PHP include. It does not have that parent/child relationship like #yield and #section.
I highly suggest reading the Laravel Blade documentation for a more comprehensive description.
#include and #yield are two completely different types of operations to import code into the current file.
#include - import the contents of a separate file into the current file at the location in which it is placed. i.e.:
Layout file:
< some html or other script >
#include('include.file_name') // "include." indicates the subdirectory that the file is in
< more html or other script >
Include File ( a blade file with a block of code ):
< some cool code here >
The contents of 'file_name' ( also a blade file ) is then imported in where the #include directive is located.
#yield imports code from a "section" in the child file ( the "view" blade file. ) i.e.:
Layout file:
< some html or other script >
#yield('needed_section_name')
< more html or other script >
The following section is needed in the "view" blade file that is set to "extend" that layout file.
"View" blade file:
#extends('layout.file_name')
... code as neeeded
#section('needed_section_name')
< some cool code here >
#stop
...
more code as needed
Now the layout file will import in the section of code that matches the naming used.
More on the subject here....
The difference between #yield and #include is how you use them.
If you have a static kind of content, like a navbar, this part of the page will always be in the same place in the layout. When you use #include in the layout file, the navbar will be put once per layout. But if you use #yield you will be enforced to make a #section of the navbar on every page that #extends the layout.
#yield is, on the other hand, a better choice when content is changing on all the pages but you still want to use the same layout everywhere. If you use #include you'll have to make a new layout for every page, because of the difference of content.
Today I was trying to figure out this difference as well, and where to use each, and why would I want to use one over the other. Please be warned this answer is verbose and probably very over-explained.
Snapey from the Laracasts forums got me started thinking about them properly:
https://laracasts.com/discuss/channels/laravel/whats-the-difference-between-atinclude-and-atyield
First off, #include is going to include an entire file, just like the PHP include function. That's great if you're just dumping an entire file of content into the <body> of your page, for example the following is going to include everything inside of 'content.blade.php':
<!-- layout.blade.php -->
<body>
#include('content')
</body>
<!-- content.blade.php -->
<div>
<div>
<p>Hey this is my content.</p>
</div>
<div>
<span>and stuff</span>
</div>
</div>
But #yield, in conjunction with #extends and the #section and #endsection directives, will allow you to have your content chunked into separate sections, but kept all in one file. Then you can #yield it into the layout in separate chunks. The visual that comes to mind is shuffling one half of a deck of cards into the other half, in a classic "riffle" shuffle:
<!-- content.blade.php -->
#extends('layout')
#section('top_content')
<h1>Hey I'm the title</h1>
#endsection
#section('middle_content')
<p>Hey how's it going</p>
#endsection
#section('other_content')
<p>It's over now.</p>
#endsection
<!-- layout.blade.php -->
<body>
<div>
#yield('top_content')
</div>
<p>Some static content</p>
<div>
#yield('middle_content')
</div>
<p>Some more static content</p>
<div>
#yield('other_content')
</div>
<div>Static footer content of some kind</div>
</body>
Secondly and maybe more importantly, the flow of control is sort of inverted, in a way that makes everything much more coherent. In the first example, with #include, you'd be calling the layout file with the view helper, in sort of a top-down way. For example this might be your code:
Route::get('/', function () {
return view('layout');
});
But with #yield and #extends, (as in the second example) you call the content file itself, and the content file will first look at the #extends directive to drape itself with the layout file, like it is putting on a coat. So it happens in reverse, in a sense, like bottom-up. Then the #yield directives inject the content as specified. The content file is who you are talking to in your router/controller:
Route::get('/', function () {
return view('content');
});
You call the content view, it looks at the #extends directive to pick the layout, and then the #yield directives in the layout file inject the matching #section sections into the layout.
So this way is much more useful because in practice you'll be referring to different content when you refer to different views.
If you were only using the #include statement to build your views, then you'd have to pass a different content slug to the layout file that you are calling every time, maybe like this:
Route::get('/welcome', function () {
return view('layout', ['content' => 'welcome']);
});
Route::get('/profile', function () {
return view('layout', ['content' => 'profile']);
});
<!-- layout.blade.php -->
<body>
#include($content)
</body>
And that seems like a mess to me.
That all being said, #include seems like a great way to include a little snippet in your layout file (the one called by the #extends directive), like the nav bar, or the footer, or something you just want to separate out of your layout file for organizational purposes.
#yield should be used when your contents will be changed
#include should be used for contents that wont change. e.g header, footer
#include used for reusable code like navbar, we have to design navbar one time and use it in our whole site.
#yield used for sections that change again and again like body.
for example you have already your layout structure where you #include('some scripts or style'). it will not allow you to change its directive while #yield you can change its content. means you create a section to yield into your layout.blade.php. you can use yield also if you have a specific script or style in each page.
#include('layouts.nav') //default when you call layout.blade.php
<div class="container">
#yield('content') //changes according to your view
</div>
#include('layouts.footer') //yes you can use #yield if you have specific script.

Include more controllers into a single page

I just started learning Laravel, and I want to include a second controller into my main layout.
The route is the default root directory: /
And the layout looks like this:
<div class="container">
#yield('content')
</div>
<div class="basket">
~basket comes here~
</div>
I want to show the user's basket, but I need DB queries for that, and I can't find a way to include an other controller, atleast not with routing.
I'm not really asking for code (Sadly i didn't find a better place for this question), probably I just need a designing tip, I really feel I'm trying to do it wrong, since I couldn't find any relevant/helpful information for this.
And I dont want to put the basket into every controller that uses my main layout.
Any kind of help would be apreciated, I'm really lost :)
You should use view composers. Open you AppServiceProvider and inside the boot() method add the following:
view()->composer('your.layout.name', function ($view) {
$basket = ...// Your basket query here
$view->with('basket', basket);
});
This basically says, when view with name your.layout.name is composed, add variable with name $basket.

Resources