yield is not showing nothing - laravel

Im with some doubts about how to strucure the views folder on laravel. For example a blog site where we have this structure in the homepage:
Header (where we have a menu with: a link to homepage, search bar to search for posts, sign in and login buttons)
Then a section with the last 5 posts.
Then a section with the most viewed posts.
And then a footer.
How we can structure this homepage in terms of views? For example maybe we can something like this:
-views
- layouts
- header.blade.php
- footer.blade.php
- posts
- lastposts.blade.php
- mostviewed.blade.php
- single.blade.php
- layout.blade.php
Do you think its ok? Because Im testing this and it is not working. For example in the layout.blade.php I have:
<!DOCTYPE html>
<html lang="en">
<head>
....
<link href="css/app.css" rel="stylesheet">
</head>
<body>
#include('layouts.nav')
#yield('lastposts')
#yield('mostviewed')
#include(layouts.footer')
</body>
</html>
And I get the header and the footer when I access the page corretly, but the both #yields dont show nothing.
In the lastposts.blade.php to show the last 5 posts I have:
#extends('layout')
#section('lastposts')
<div>
<h4>Title</h4>
<p>Text</p>
</div>
</div>
<div>
<h4>Title</h4>
<p>Text</p>
</div>
</div>
<div>
<h4>Title</h4>
<p>Text</p>
</div>
</div>
<div>
<h4>Title</h4>
<p>Text</p>
</div>
</div>
<div>
<h4>Title</h4>
<p>Text</p>
</div>
</div>
#endsection
I have the same logic for the mostviewed.blade.php, but again, the #yield('mostviewed') dont show nothing.
Do you know what is not corret?
Routes file:
Route::get('/', function () {
return view('layout');
}
I have just this route because I just have the homepage for now.

You created the wrong view. The parent view is layout, it doesn't know about mostviewed and lastposts. That's why you wrote #extends('layout'), that way when you create your content view, it will know that it has to extend stuff from layout.
Route::get('/', function () {
return view('posts.mostviewed');
}

Related

Laravel 8.0, problems with #yield('content') make duplicity of the content section in other div

I have a newly started project in Laravel 8.0, and I have a problem with views.
I have the following master.blade.
<html>
<head>
<title>App Name - #yield('title')</title>
</head>
<body>
<div class="container">
#yield('content')
</div>
<footer class="row">
#include('layouts.footer')
</footer>
</body>
</html>
I then have a view that extends that master blade:
#extends('layouts.app')
#extends('layouts.master')
#section('content')
HELLO
#stop
The problem is, when rendering the view, the information is duplicated. Appears once inside the container div so the #yield works, but is re-rendered in another div outside the container (main class="py-4").
Let's see if someone can help me with the problem.
Thank you very much in advance.
#extends('layouts.master')
#section('content')
HELLO
#endsection
you extends two time
You must add one extends, edit the code and do the code below
#extends('layouts.master')
#section('content','your title')
HELLO
#endsection
that the right way to get the view.

How to switch root view of inertia in laravel?

I have a website where I want to setup multiple layouts.
auth layout : for (login, register etc) pages
dashboard layout: for authenticated users to show dashboard and all other pages related to it there.
I have tried to change root view in controller but it didn't work
Inertia::setRootView('dashboard');
dashboard.blade.php : it contains sidebar for logged in users and have different style theme
<!doctype html>
...
<div class="rt-wrapper">
#include('partials.sidebar')
<main id="rt-main" class="rt-main rt-haslayout">
<div class="rt-dashboard">
#inertia
</div>
</main>
</div>
...
</html>
auth.blade.php : it only contains login, register etc pages and no sidebar with different style theme.
<!doctype html>
...
<div class="rt-wrapper rt-wrappernopadding">
<div class="rt-loginpagewrapper">
#inertia
</div>
</div>
...
</html>

Laravel Blade #yield is not working inside included view

I am trying to make layout using blade but the problem is that when i tried to
#yield on the file which is included in master file but #yield is not working.
resouces/views/layouts/app.blade.php
<html>
<head>
...
...
</head>
<body>
#include('layouts.navigation')
#include('layouts.main_panel')
#include('layouts.footer')
</body>
</html>
resouces/views/layouts/main_panel.blade.php
// some html stuff
#yield('form')
// some html stuff
resouces/views/auth/login.blade.php
#extends('layouts.app')
#section('form')
<form>
// input
</form>
#endsection
I would suggest you pass variables to the partials and echo
them inside it. This is an alternative way to achieve what you are
trying to do.
For example -
Partial blade file (resouces/views/partials.header.blade.php) -
<h4>{{ $name }}</h4>
View (resouces/views/custom.blade.php) -
#include('partials.header', [ 'name' => 'Lorem Ipsum' ])
I am also using laravel framework but i used to do in that way :-
Layout :- resouces/views/layouts/app.blade.php
<html>
<head>
...
...
</head>
<body>
#include('layouts.navigation')
#yield('content') // use #yield here why you need separate file
#include('layouts.footer')
</body>
</html>
After that :- resouces/views/auth/login.blade.php
#extends('layouts.app')
#section('content')
<form>
// input
</form>
#stop
Hope it helps!.. I used to follow this structure in laravel project

Laravel blade template yield in child

I've a master layout like this:
<head>
#yield('styles')
</head>
<body>
#include('header')
<div class="container-fluid">
#yield('content')
</div>
#yield('scripts')
</body>
Now I've following structure in page content:
#section('content')
<div class="page-content">
#include('sidebarandfooter')
</div>
#endsection
#section('copyright')
#include('copyrightv2')
#endsection
Sidebarandfooter.blade.php have following:
...[CODE for sidebar]...
#yield('copyright')
It's should be called inside <div class="container-fluid"></div> as I've different class of div container for different pages.
I'm not able to yield the copyright part. I've different copyright section for different pages. Is it wrong, how can we execute such kind?
Unfortunattely it's imposible in way you declare it because this section:
#section('copyright')
#include('copyrightv2')
#endsection
been loaded before you call this: #include('sidebarandfooter') with copyright section inside.
What you can do is to pass a key as a parameter to view to the included page content partial like this:
#section('content')
<div class="page-content">
#include('sidebarandfooter', ['copyrightsView' => 'copyrightv2'])
</div>
#endsection
and then just call inside Sidebarandfooter.blade.php in the include:
...[CODE for sidebar]...
#include($copyrightsView)
change your master layout like
<head>
#yield('styles')
</head>
<body>
#include('header')
<div class="container-fluid">
#yield('content')
</div>
#yield('copyright')
#yield('scripts')
</body>
since i am not seeing a section for copyright in your master layout

Blade template. Understanding manual or examples for use with several sections

I can't looking for solution or understanding solution when I try use blade (laravel 4.2) for use with several sections.
For normal use (home extendes layout) not problem.
views/home.blade.php
#extends('layout')
#section('content')
Show content of home page
#endsection
view/layout.blade.php
<html>
<!-- Code html such head, template header... -->
...
#yield('content')
<!-- Rest code html such footer -->
...
</body>
</html>
But when I like move Rest code (footer) to another template file footer.blade.php I don't understand and try several ways.
view/footer.blade.php
#extends('layout')
#section('footer')
<hr>
<footer>
<p>© Tamainout Hébergement SARL 2015</p>
</footer>
</div>
#endsection
and change views/home.blade.php
<html>
<!-- Code html such head, template header... -->
...
#yield('content')
#yield('footer')
<!-- Rest code html such footer -->
...
</body>
</html>
But file footer.blade.php it isn't processed by laravel.
Apreciate some help
NOTE: I put code on github, abkrim/blade (only files envolved)
I think you have a syntax error in the views/home.blade.php.
Try:
#yield('footer')
instead of
#yield(footer')
EDIT:
include footer.blade.php in the layout.blade.php and remove extends from footer.blade.php
views/home.blade.php:
#extends('layout')
#section('content')
Show content of home page
#endsection
views/footer.blade.php:
<hr>
<footer>
<p>© Tamainout Hébergement SARL 2015</p>
</footer>
</div>
views/layout.blade.php:
#yield('content')
#include('footer')

Resources