Laravel yield is empty - php

I have a master blade file that includes some other blade files and yields content of a blade file called home which I extend on this master blade, I can click on master from my home blade in phpstorm and it takes me to the correct file, so they must be connected correctly.
The problem is the contents are not loaded, it just loads the head/header and footer but nothing from my home.blade.
My master.blade.php:
<!doctype html>
<html lang="en">
#include('partials.head')
<body>
#include('partials.header')
#yield('content')
#include('partials.footer')
</body>
</html>
In my home.blade.php
#extends('master')
#section('title', 'Home')
#section('content')
<main id="content">
test
</main>
#endsection
It loads everything correctly except the content section. What am I doing wrong?
Only route I got:
Route::get('/', function () {
return view('master');
});

You have to change the view of the route,
Route::get('/', function () {
return view('home');
});

Related

Blade template - #extends & #section not working

I'm learning the laravel framework and trying to get to grips with using the blade template engine. However i cant for life of me get the #extends and #section functionality to work within my project.
I have already tried reinstalling the whole project multiple times, using different browsers and restarting my machine but i cant figure out why it doesn't display the #section content
Laravel Version: 5.7.28 |
IDE: PhpStorm
routes/web.php
Route::get('/', function () {
return view('layouts/index');
});
views/layouts/index.blade.php
<body>
<div class="container-fluid">
<h1>Site Index</h1>
#yield('header')
</div>
</body>
views/header.blade.php
#extends('layouts.index')
#section('header')
<p>Header</p>
#endsection
At the moment all that is being displayed is the tag in the views/layouts/index.blade.php file.
Thank you very much for any and all input on this.
That's not how the templating works. You have to reference the child template in your return statement. Because the #extends is in this child template, Laravel knows to use the mentioned master layout. So your return statement would be like so:
return view('header');
If you just want the header to be displayed on every page, you don't need to extend the master layout in your header, you should just include the header part in your master layout.
<body>
<div class="container-fluid">
<h1>Site Index</h1>
#include('header')
</div>
</body>
i have tested the view and layout they seems working. check your controller return statement. try return view('header');
Route::get('/', function () {
return view('header');
});
thanks all for your responses, now i understand how the blade template engine works a little better and how i was doing this wrong. Just for clarification for others that get confused like me and come across this thread:
When you are redirecting to a view through the web routes then it has to be a child that is extending from a layouts master.
routes/web.php
Route::get('/', function () {
return view('index');
});
The html from the master file will then be displayed by default and its the content that we are "viewing"
views/layouts/master.blade.php
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>#yield('title', 'default title if unspecified')</title>
</head>
<body>
<h1>Master Header</h1>
#yield('content')
</body>
</html>
To work with the content of the page then its the index view that is worked with using the #section('content') method.
views/index.blade.php
#extends('layouts.master')
#section('title', 'Changing the default title')
#section('content')
<p>content displayed</p>
#endsection
I hope this helps for anyone else.
If you want to show content of section('header') then you must return header view like
Route::get('/', function () {
return view('header');
});
this is because contents are in header view and you have been extending layout.index
so if you return layout.index view you will not see content of section('header')

Include template is not showing content when page called?

I want to call content.blade.php for both ajax and url direct calling ! If I called as ajax , I don't want to include master.blade because it has jquery and many basic link ,it will cause duplicate file ! When I called as direct link in browser , I need to include master.blade . But the content page is not showing (eg.This is content) when I called as direct url in browser ,ajax call has no problem !! How can I solve this ?
Say test action,
public function test(Request $request) {
return view("content",[ajax,$request->ajax()]);
}
master.blade.php
<html>
<head>
<title>#yield('title')</title>
</head>
<body>
#section('sidebar')
This is the master sidebar.
#show
<div class="container">
#yield('content')
</div>
</body>
</html>
content.blade.php
#includeWhen(!$ajax,"master")
#section('content')
This is content
#endsection
It's not showing because you're calling the #section('content') but you have no template that is yielding it.
You could make another layout without all the scripts that you don't want:
ajax.blade.php
#yield('content')
And then in your content.blade.php, extend the master.blade.php when it's not ajax or extend the ajax.blade.php when it's ajax:
content.blade.php
#extends($ajax ? 'ajax' : 'master')
#section('content')
This is content
#endsection

Laravel layouts not working

I am trying something really simple and can not get it to work.
I have 2 pages. admin.blade.php and left.blade.php
I am trying to use admin page as the master page. and include date from left.blade.php
The admin pages print only "test" as the result and includes nothing from left.admin.php.
I can`t see what is wrong. Thanks in advance
File structure is
-- resources
--views
*admin.blade.php
*left.blade.php
left.blade.php
#extends('admin')
#section('content')
baran
#stop
admin.blade.php
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<meta charset="UTF-8">
<title> #yield('title')</title>
</head>
<body>
<?php
// put your code here
?>
#yield('content')
test
<div id='footer'>
#yield('footer')
</div>
</body>
</html>
route command in web.php is
Route::get('/admin', function () {
return view('admin');
});
If you want to include date from left.blade.php you should use #include() directive in admin.blade.php:
#include('left')
If your main content is in left.blade.php and you're using admin.blade.php as layout only then change you route:
Route::get('/admin', function () {
return view('left');
});
You want to call the view for the inner page, not the master page, since the inner page extends the master page:
return view('left');

Laravel blade templates error

I have this code:
in laravel/resources/views/users.blade.php
#extends('layouts.main')
#section('content')
<p>
Here's your content
</p>
#stop
in laravel/resources/views/layouts/main.blade.php
<html>
<head>
{{-- Common Header Stuff Here --}}
</head>
<body>
<div class="navigation">
#section('navigation')
Home
Contact
#show
</div>
<div class="container">
#yield('content')
</div>
in routes.php
Route::get('users', function()
{
return View::make('users');
});
When i launch my site (localhost/laravel/public/users) It prints only:
#extends('layouts.main')
what's wrong here? I'm using laravel 5
Thanks in advance, i'm newbie with laravel
FIIIIIIIIXEEED
#extends can't be indented, you can't put anything before it even whitespace.
I think in Laravel 5 you cannot use return View::make('users'); That's only for pure HTML content (already compiled). If you want to use blade templates, you should instead use :
return view('users');

Exclude a view from master layout?

I have login.blade.php in views/users/ that I would like to exclude from the master layout that I have.
Instead I want the login page to be a standalone page with just the login form on it.
How may I achieve that?
Use a different layout for login page:
File app/views/login.blade.php:
#extends('layouts.standalone')
#section('content')
...
#stop
And for your other pages:
File app/views/home.blade.php:
#extends('layouts.master')
#section('content')
...
#stop
And here your layouts:
File app/views/layouts/standalone.blade.php:
<html>
<body>
This is a master layout
#yield('content')
</body>
</html>
File app/views/layouts/master.blade.php:
<html>
<body>
This is a standalone layout
#yield('content')
</body>
</html>

Categories