Blade template not showing section - php

I have these three blade templates
In my controller I'm calling dashboard view like this:
View::make('layouts.dashboard');
master.blade.php
<?= stylesheet_link_tag() ?>
<?= javascript_include_tag() ?>
<html>
<body class="fixed">
<div class="wrapper">
<div class="leftside">
#yield('leftside', 'left-side content')
</div>
<div class="rightside">
#yield('rightside', 'right-side content')
</div>
</div>
</body>
dashboard.blade.php
#extends('layouts.master')
#section('leftside')
#yield('sidebar', 'sidebar content')
#stop
sidebar.blade.php
#extends('layouts.dashboard')
#section('sidebar')
aaa
#stop
The dasbhoard blade shows properly in master blade, but sidebar blade doesn't want to show. Does anyone know what i'm doing wrong?
Thank your very much for any help :)

I don't think you can use yield() on a blade template that is not called.
Basically what you can do is make your master.blade.php into this
<html>
<body class="fixed">
<div class="wrapper">
<div class="leftside">
#include('sidebar') {{-- This is sidebar.blade.php --}}
</div>
<div class="rightside">
#include('dashboard') {{-- This is dashboard.blade.php --}}
</div>
</div>
</body>
</html>
Then, assuming that your are calling the dashboard.blade.php template, with both blade templates included. You can use the sections on either master.blade, or sidebar.blade inside dashboard.blade
#extends('layouts.master')
{{-- You can define now the sections available on master.blade and dashbard.blade --}}

OK I found out what was wrong :)
I called View::make('layouts.dashboard') instead of View::make('layouts.sidebar');

Related

Include blade file from another file not working

I am trying to include this comments.index file:
#extends('posts.show')
#section('title', 'Comments')
#section('comments')
Create Comment
#foreach ($comments as $comment)
#if ($comment->post_id == $post->id)
<div class="border margin mb-1">
<div class="border margin">
<p>{{$comment->text}}</p>
</div>
Edit
</div>
#endif
#endforeach
Back
#endsection
From this posts.show file:
#extends('layouts.myapp')
#section('content')
<head>
<link href="{{ asset('css/posts.css') }}" rel="stylesheet">
</head>
<div class="post_container">
<div id="post_title">
{{$post->title}}
</div>
<div id="post_text">
{{$post->text ?? 'Empty'}}
</div>
<div>
<img src="{{ asset('storage/images/'. $post->image) }}">
<p>
{{$post->image}}
</p>
</div>
</div>
<div>
<h2>Comments</h2>
<div>
#include('comments')
</div>
Comments
</div>
Back
#endsection
Now the issue is that when I include 'comments' it throws 'view comments not found' error and asks if I am sure there is a blade file with such a name. This made me think that I need to include 'comments.index', but when I do this I get 'undefined variable $comments' error.
Doing an include of a partial is dot notation. So in this example...
#include( 'includes.modals.dude' )
This is really located in views -> includes -> modals -> dude.blade.php
The file name must be YourName.blade.php.
I think you are providing a relative path, include directive needs absolute path. So, in your case following code should work.
#include('comments.index')

Yield blade file from another file not working

I am trying to display comment index content
#section('title', 'Comments')
Create Comment
#foreach ($comments as $comment)
#if ($comment->post_id == $post->id)
<div class="border margin mb-1">
<div class="border margin">
<p>{{$comment->text}}</p>
</div>
Edit
</div>
#endif
#endforeach
Back
#endsection
From posts show file:
#extends('layouts.myapp')
#section('content')
<head>
<link href="{{ asset('css/posts.css') }}" rel="stylesheet">
</head>
<div>
<h2>Comments</h2>
<div>
#include('comments.index')
</div>
Comments
</div>
Back
#endsection
I want to be able to see the comments without having to go on comments.index link, which I have to delete.
It's only a blind guess: maybe you should look at your blade directory structure, where the file with the comments-section is located, maybe it should be
#yield('posts.comments')
And by the way the head tag in the content section looks a bit displaced imho.
The problem is that you are not yielding data here. You just include the value here. So use #include in stead of #yield. The following code is below:
In comment file :
Create Comment
#foreach ($comments as $comment)
#if ($comment->post_id == $post->id)
<div class="border margin mb-1">
<div class="border margin">
<p>{{$comment->text}}</p>
</div>
Edit
</div>
#endif
#endforeach
Back
Now in the From posts show file:
#extends('layouts.myapp')
#section('content')
<head>
<link href="{{ asset('css/posts.css') }}" rel="stylesheet">
</head>
<div>
<h2>Comments</h2>
<div>
#include('comments')
</div>
Comments
</div>
Back
#endsection

Laravel #yield is undefined for HTML section

The layout/template file is defined in footer.blade.php:
#extends('mainpage')
#section('footer')
<section class="section5">
<footer class="container col-lg-12 pb-2 pt-4">
<ul class="nav justify-content-center border-bottom pb-3 mb-3">
<li class="nav-item">Home</li>
<li class="nav-item">Features</li>
<li class="nav-item">Pricing</li>
<li class="nav-item">FAQs</li>
<li class="nav-item">About</li>
</ul>
<div class="row">
<p class="small-text col-lg-3 text-lg-start text-center">© MyCompany.com #php echo date("Y") #endphp </p>
<p class="small-text col-lg-6 text-lg-center text-center">All trademarks used on this site are the property of their respective owners.</p>
<p class="small-text col-lg-3 text-lg-end text-center">Terms & Conditions | Privacy Policy</p>
</div>
</footer>
</section>
#endsection
This is then yielded in mainpage.blade.php:
<!-- REST OF HTML -->
#yield('footer', 'Undefined')
</body>
Both files exist in the same directory, and there is also a route for /mainpage defined in web.php:
Route::get('/', function () {
return View::make('mainpage')->render();
});
Yet the only thing that navigating to /mainpage shows me is the Undefined "error" that I passed to it as the second argument to the #yield helper. Inspecting in the browser shows that the section isn't loaded.
Because the template inheritance fails silently without an error and because Laravel apparently doesn't even have an intuitive way of printing warnings, such a basic use case seems impossible for me to debug.
What's going on here, or alternatively, how would I begin to go about debugging it?
For your current view to work, your route would need to call footer instead of mainpage.
footer.blade.php knows what view it extends.
<!-- footer.blade.php -->
#extends('mainpage') <!-- Ok, extend mainpage.blade.php -->
#section('footer') <!-- place this section where there's a #yield('footer') in mainpage.blade.php -->
#endsection
mainpage.blade.php doesn't know what views will extend it.
<!-- mainpage.blade.php -->
#yield('footer', 'Undefined') <!-- Place 'Undefined' if the views that extend me don't have a #section('footer') -->
I could make a second view called footer2.blade.php, and also have it extend mainpage. In that situation, which view should mainpage render?
What you want for generic sections like footers is including a subview (using #include).
Here's a crude example:
<!-- layout-1.blade.php. This layout has a navbar, sidebar and footer. -->
<html>
<head>
</head>
<body>
<!-- layout-1 navbar -->
#include('navbar')
<!-- layout-1 sidebar -->
#include('sidebar')
<main>
#yield('content')
</main>
<!-- layout-1 footer -->
#include('footer')
</body>
</html>
<!-- layout-2.blade.php. This layout has a navbar and a footer but no sidebar -->
<html>
<head>
</head>
<body>
<!-- layout-2 navbar -->
#include('navbar')
<main>
#yield('content')
</main>
<!-- layout-2 footer -->
#include('footer')
</body>
</html>
#extends('layout-1')
#section('content')
#endsection
#extends('layout-2')
#section('content')
#endsection

Inheritance with Laravel 5

I have a master_page.blade with global content for my html:
<!DOCTYPE html>
<html lang="es">
<head>
#include('layout.head')
</head>
<body>
#include('layout.header')
#yield('master_content')
#section('footer')
#include('layout.footer')
#show
#include('layout.scripts')
#yield('custom_scripts')
</body>
</html>
After, I have a layout in Bootstrap with 2-6 columns, this blade inherit of the master_blade, his name is 2_10_columns.blade:
#extends('layout.master_page')
#section('master_content')
<div class="container">
<div class="main-content">
<div class="col-sm-2">
#section('content1')
#show
</div>
<div class="col-sm-10">
#section('content2')
#show
</div>
</div>
</div>
#stop
Ok, this looks good but, finally I need fill the 2_10_columns.blade with some content, for example I tried this with mi contact.blade.php:
#extends('layout.2_10_columns')
#section('content1')
<p>Column 2 left content of my section...</p>
#stop
#section('content2')
<p>Column 10 right content of my section ...</p>
...some forms here
#stop
Definitely this does not work...
How could I do this with Laravel? Look at this infographic:
its working like this(simplified for example):
master layout:
<html>
<body>
<div class="container">
#yield('master_content')
</div>
</body>
</html>
2 cols layout:
#extends('layouts.master')
#section('master_content')
<div class="co-l2">
<p>index</p>
#yield('sidebar')
</div>
<div class="col-10">
#yield('content')
</div>
#endsection
contact view:
#extends('layouts.2cols')
#section('sidebar')
<p>delete</p>
#endsection
#section('content')
<h1> person name </h1>
<p>about the person</p>
#endsection

Laravel #section

My view folder structure
content
home.blade.php
includes
partials
mainsection.blade.php
layout
default.blade.php
My home.blade.php
#extends('layout.default')
#section('content')
#section('partials.mainsection')
<div class="col-xs-10 col-xs-offset-1 col-md-8 col-md-offset-2 text-center">
<h1>Some Text</h1>
</div>
#stop
My mainsection.blade.php
<section class="section">
<div class="container">
<div class="row">
#yield('section')
</div>
</div>
</section>
My default.blade.php
#include('layout.header')
<div id="wrapper">
<header>
<div class="container">
#include('partials.mainNav')
</div>
</header>
<div id="main">
#yield('content')
</div>
#include('layout.footer')
</div>
My thought was that i can take the partials.mainsection view for my content so that i dont have to write everytime the section / container / row code.
So i only write some text in #section('partials.mainsection') which is wrapped by the mainsection view.
But this does not work, what do i wrong or is there any other way to do this ?
The "partials.mainsection" is only my wrapper for my content(s) so i don't have to write this every time again. If i use just #include i can't extend it from each view.
To include a partial or any other partial into your blade template the syntax is #include('view.name')

Categories