Laravel cannot seem to yield a section - php

Having a bit of a brain fart. Can't figure out why my section is not being included
So I'm trying to include the section scripts in my footer, but I get nothing when I reload the page. It is definitely being loaded, just the yield is not working
So here's my view:
#extends('layouts/form')
#section('scripts')
<script>
alert('It works!');
</script>
#stop
Here is layouts/form
#include('layouts/head')
#include('layouts/header')
// html stuff
#include('layouts/footer')
And my footer:
// footer html
#yield('scripts','')
</body>
</html>
Is my understanding of sections correct? If the section scripts does not exist then it simply prints out nothing. Does it need to be in a certain order?

From my understanding of the blade template engine:
<div class="col-xs-12">
#yield('content')
</div>
Is valid, and anything that extends the file containing this #yield (in my case master.blade.php) will print it's content there:
#section('content')
<div class="container-fluid">
#include("order-form")
</div>
#stop
The only other thing I can think of (after getting a #yield('scripts') to work flawlessly on one of my projects is the structure. To give you an idea, this is my structure for app/views:
..
-> forms
-> generic.blade.php
-> loss.blade.php
-> rts.blade.php
-> layouts
-> master.blade.php
index.blade.php
order.blade.php
layouts.master -> #yield('content') -> index.blade.php -> #section('content') -> #include('forms.loss') -> #stop
That should make the relationships pretty clear.. Maybe that helps, I dunno. It's tricky, and I feel like it should work in your case. Let me know if that helps.

It was actually because I am have some pages being loaded from a database which tells laravel which layout to use, so the script section was never in them that view

Related

Blade does not seem to work (maybe "extends")

I know it's not a specific question, but that's because it's the only info I have about my problem.
For some reason, my blade templates do not seem to work.
Folder structure:
resources / views / layouts / master.blade.php
resources / views / child.blade.php
master.blade.php:
<p>Some content here</p>
#yield('content')
<p>Some additional content here</p>
child.blade.php:
#extends('layouts.master')
#section('content')
<p>This is the user content</p>
#endsection
My expectations are:
Some content here
This is the user content
Some additional content here
What I get is:
Some content here
Some additional content here
So my content section is not showing up at all.
I am using Laracast and Laravel v5.7.8
I also set up a new laravel application in different versions (5.1.* - 5.7.8) but in every new application it didn't work.
I think it can't be an issue with my files. Let me tell you why:
The welcome.blade.php shows a login button if Route::has('login').
If I add a login route it doesn't show aswell and I didn't edit the welcome page. And it does not work in new laravel applications aswell.
I don't got a clue why this is...
Thanks for any help.
And yes, I've done my research for a couple of hours now.
For the order you mentioned you just need to move your yield below the paragraph, like this:
<p>Some content here</p>
<p>Some additional content here</p>
#yield('content')
Notice how #yield is the below these 2 paragraphs, with this you get the desired output
I got it now. The routes where wrong and nothing with my files.
What I did was:
Route::get('/', function () {
return view('master');
});
This is of course wrong and what it should be is:
Route::get('/', function () {
return view('child');
});
I've looked through the files many times and didn't notice it...
It's just some of those days :D.
Still, thanks to anyone who tried to help.

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

#extends('layout') laravel. Blade Template System seems like Not working

I tried to use the laravel's template system: blade but seems like not working when using the code below in the file users.blade.php:
#extends('layout')
#section('content')
Users! #stop
and browser,
#extends('layout')
That should work if you have a template file at /app/views/layout.blade.php that contains
<p>Some content here</p>
#yield('content')
<p>Some additional content here</p>
Then in your /app/views/user.blade.php, the content
#extends('layout')
#section('content')
<p>This is the user content</p>
#stop
If you call return View::make('user') you should have the compiled content
<p>Some content here</p>
<p>This is the user content</p>
<p>Some additional content here</p>
I hope that helps clarify things for you. If not, can you provide your template file locations and the relevant content?
Just remove the extra space or anything before #extends('yourlayoutfile').
It should be the first thing to be rendered in the file.
I was facing the same problem and tried many things.Suddenly I found a single space at the starting of the file before #extends.
Removed the space and is working fine.
Thanks.
Format:
#extends('layouts.default')
#section('content')
.....
#stop
---Edit----
If this didnt work then try :
Copy all the content in the file and then delete the file.
Create a new file and save it as filename.blade.php
Only after saving the file paste the content into the page.
Save the changes and run it.
This works.
Thank you.
Where is your layout?
If its in app/views/layouts, then it should be
#extends('layouts.index')
(assuming the name is index.blade.php)
ex: #extends('layouts.foo') equals a file in app/views/layouts/ called either foo.blade.php or foo.php. (depending if you use blade)
I have the same problem. What is did is:
1. in routes.php
Route::get('about', 'AboutController#index');
that
AboutController is a controller file AboutController.php in app/controllers
index is a function inside that controller.
2.Create AboutController.php in app/controllers
class class AboutController extends BaseController {
protected $layout = 'layouts.default';
$this->layout->content = View::make('pages.about');
}
You can look at this reference: Defining A Layout On A Controller
By default,Laravel has a layouts folder inside views folder, i.e. app/views/layouts and in this folder you keep your layout files, i.e. app/views/layouts/index.master.php and if you have something similar then you should use something like this:
#extends('layouts.master')
#section('content')
<p>Page Content</p>
#stop
This will inherit/use the master.blade.php file (as layout) from layouts folder, here, layouts.master means layouts/master.blade.php.
In your master.blade.php file you mast have this
#yield('content')
So, data/content from the view between #section('content') and #stop will be dumped in the place of #yield('content') of your layout.
You can use any name for your layout file, if it's layouts/main.blade.php then you should use
#extends('layouts.main')
Make sure you inserted the css link in App.blade.php
For me By default there is no link to the css file
Insert the following link in app.blade.php
<link rel="stylesheet" href= "/css/app.css" >
now its works fine :)
list things to make sure
file name and path properly given
double-check .blade.php file extention
layouts.admin.blade.php
<section class="content" style="padding-top: 20px">
#yield('content')
</section>
#extends('layouts.admin')
#section('content')
<p> this is Order index view</p>
#endsection
Try this!
php artisan cache:clear
php artisan view:clear
php artisan route:clear
php artisan clear-compiled
let's say you have 'master.blade.php' and 'index.blade.php'.
and both of files are in views->home directory. when you want to use #extends in 'index.blade.php' by calling 'master.blad.php' , you should write in index.blade.php file this statment:
#extends('home.master')
not
#extends('master')
Simply save your source using encoding UTF-8 without signature.

Laravel 4 include multiple views in route

Is it possible to include multiple view in route? What is the best practice for doing this, let say I want config file, header, content, and footer file to join and load in a view? If do it in the route, then I can easily change the content based on route request.
Thank you.
You need to take a futher look at Laravel's Blade templating. With Blade templating, you can create layouts and cascade them onto each other really nicely. For example, let's take the following routes...
app/routes.php
Route::get('about', function()
{
return View::make('about');
});
Route::get('contact', function()
{
return View::make('content')
});
As you can see, we have two different views for those two different request. However, with Blade templating, and sections, we can create a master layout and only change the content that we need. So, here is what our master layout would look like.
app/views/layouts/master.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Site Title</title>
</head>
<body>
<nav>
<ul>
<li>Home</li>
<li>About</li>
<li>Contact</li>
</ul>
</nav>
#yield('content')
</body>
</html>
This is our master layout. We have our nav that will always stay the same, our HTML and head and everything that we don't want to write over and over again. But, we are also using yield in blade to accept content and place it there. This is where our actual views come into play from routes.php.
app/views/about.blade.php
#extends('layouts.master')
#section('content')
<p>This is the about me content.</p>
#endsection
We can simply extend the master layout, and place our content within the content section, which we can name anything we want. Same with the other page, contact.
app/views/contact.blade.php
#extends('layouts.master')
#section('content')
<p>This is the contact page content.</p>
#endsection
As you can see, it's not so much as including multiple views...but rather it's about extending different views and putting them together using Blade.
Alternate way to include header / footer in another view will as below.
https://laravel.com/docs/5.3/blade#including-sub-views
<div>
#include('templates.header') // views/templates/header.blade.php
<form>
<!-- Form Contents -->
</form>

Categories