Laravel: extend a file in parent folder from subfolder - php

i want to extend the master.blade.php located in views\backend folder.
view/backend/master.blade.php <--i want to extend this file
view/backend/partials/header.blade.php <--the file that will extend the master.blade.php
folder structure
views
backend
-master.blade.php
partials
-header.blade.php
-footer.blade.php
-sidebar.blade.php
I tried this in the header.blade.php file but fail:
#extends('backend.master')
Edited
master.blade.php
<body>
<div class="wrapper">
#yield('header')
#yield('sidebar')
#yield('content')
#yield('footer')
</div>
</body>
header.blade.php
#extends('backend.master')
#section('header')
<p> this is the header</p>
#endsection
The page shows master content only in the browser

Normally header, footer and sidebar contain the markup which remain common across pages. Only the content varies from page to page.
Also the concept of extending master layout is to avoid repeating the shared parts across various pages. Using #yield('header') #yield('footer')
#yield('sidebar') defeats the concept of extending master layout. Because then all those sections need to be included on all pages.
So you master layout should be like
<body>
<div class="wrapper">
#include('backend.partials.header')
#include('backend.partials.sidebar')
#yield('content')
#include('backend.partials.footer')
</div>
</body>
Then for any page you can extend the master layout as
#extends('backend.master')
#section('content')
<!-- content markup here -->
#endsection

Related

When extending a layout all the #yield are rendered instead of the #sections chosen

When trying to use #yield and #section it does not work. With only extending the layout, all the layout is rendered, I can't choose with #section and #endsection what will be rendered.
This is for a Laravel project with HomeStead on my local machine
plantilla.blade.php is:
<html>
<head>
<title>App Name - #yield('title')</title>
</head>
<body>
<div>
#yield('sidebar')
This is the master sidebar.
</div>
<div class="container">
#yield('content')
This is another container
</div>
</body>
</html>
And the contact.blade.php is:
#extends("layouts.plantilla")
#section('content')
#endsection
When opening contact.blade.php, both sections are displayed (sidebar AND content), instead of only content, which is the section I'm actually calling.
This happens also if I just leave the first line (#extends("layouts.plantilla")
without calling any section, it will render all of its content
What could am I doing wrong here?
That's because you are yielding both content and sidebar inside your div already, so the "This is the master sidebar" and "This is another container" will always show, even if you don't use that section.
You need to change your code in main layout like this:
<html>
<head>
<title>App Name - #yield('title')</title>
</head>
<body>
#yield('sidebar')
#yield('content')
</body>
</html>
So now, if you only want to display a content of your page, you do it like this:
#extends("layouts.plantilla")
#section('content')
<div class="container">
This is another container
</div>
#endsection

Default view for all routing in laravel

I'm starting with laravel. Now have created an admin Panel design.
I want to display one view (menu in the left side for admin Panel) in default for all routings started on /admin
Then load the content of the container depending on the route.
Do I have to set in all routes my view for left side or I can set it "globally" for all routes?
Laravel version I'm using is 5.7
You have just to create a layout for the admin, and all controller related to admin action will render view which extend the admin layout which you have define.
The layout file admin.blade.php
<html>
<head>
<title>App Name - #yield('title')</title>
</head>
<body>
<div id="menu">
<!-- use the css to style this element on the left -->
<ul>
<li>Your Menu code goes here</li>
</ul>
</div>
<!-- Here is where all content related to each route will be rendered -->
<div class="container">
#yield('content')
</div>
</body>
</html>
If you have a Controller which allow you to Edit Post you can render a view admin/edit.blade.php which is define like this
#extends('admin.blade.php')
#section('content')
Content to be display related to each route goes here
#endsection
The content which is wrap within the #section('content') will be render in the admin.blade.php which is the admin layout in the the container which wrap the #yield('content)`

Blade syntax #extends loads second HTML view and then the first HTML view

I am new to laravel and blade template engine.
Issue: file form.blade.php
#extends('front.header')
#extends('front.footer')
Loads front/footer.blade.php first and then the contents of front/header.blade.php
Please find attached the snap shot of the View Source.
I have checked few answers in stackoverflow they say about the white space.I dont seem to have any.
Regards,
Jignesh
This is how I would recommend structuring your master template.
front._layouts.master:
<!DOCTYPE html>
<html>
<head>
#include('front._layouts.head')
</head>
<body>
<header>
#include('front._layouts.header')
</header>
<main>
#yield('content')
</main>
<footer>
#include('front._layouts.header')
</footer>
#stack('scripts')
</body>
</html>
Notice the #stack() this can come in useful when you're making a robust part of an application. Stacks allow you to push to named stacks on top of each other.
Follow this steps:
step 1:
resources\views create a file like: master.blade.php
step 2:
resources\views create a folder like: layouts
Inside layout folder create your header & footer file
step 3:
inside master.blade.php write how you design your main template like so.
<!DOCTYPE html>
<html lang="en">
<head>
<!-- your common css here -->
#yield('partial-css')
</head>
<body>
#include('layouts.top-header')
<div class="container">
#yield('content') <!-- this is your common body -->
</div> <!-- /container -->
#include('layouts.footer')
<!-- your common js here or you also define inside the footer page -->
#yield('script') <!-- this is your individual script for all page -->
</body>
</html>
Step 4:
Now you use master page for all other pages like so index.blade.php
#extends('master')
#section('content')
<!-- Here is your main body content -->
#endsection
#section('script')
<!-- Here is your individual script content -->
#endsection
Hope you understand now how blade template works!
You cannot #extends from 2 parents. If you want to include another view, you should use #include instead
Like this :
#include('front.header')
Content
#include('front.footer')

Laravel template showing two blade templates from route

I have a full html template, that I am trying to use with laravel.
The template has a big image slider (that should only be in homepage) and a couple of other codes like contact form, accordion, twitter widget... my goal is to place those parts in separate blade templates and call them when needed. For this, I have this folder scheme.
..
/views
/emails
/home
- slider.blade.php
- contact-form.blade.php
- twitterwidget.blade.php
/layouts
- master.blade.php
So, for example master.blade.php looks like this.
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div class='header'>
//header
#yield('slider')
</div>
<div class='content'>
//content
#yield('contact-form')
</div>
<div class='footer'>
//footer
</div>
</body>
</html>
Now that is a basic examples which yields a slider inside the headers tags, and this is the slider.blade.php:
#extends('layouts.master')
#section('details')
<div class='slider'> I am a slider </div>
#stop
but when I create a route, I am forced to create one point to one template. as:
Route::get('/', function(){
return View::make('home/slider');
});
This only renders the layout by pulling the slider. But I want to contact form to be rendered also.
In master.blade.php file instead of
#yield('contact-form')
you should use:
#include('home.contact-form')
or you can edit your slider.blade.php file and add at the end:
#section('contact-form')
#include('home.contact-form')
#stop

Laravel once defind yield content

I a'm trying to yield my content from controllers. But I don't want to define again and again that I want to yield the 'Content' section
How can I archive this, so I don't need to place
#section('body')
<h1>Content</h1>
<p>More content</p>
#stop
again is every view
For example, ASP.NET MVC with RenderBody()
Use Blade templating engin, create a master layout in your `app/views/layouts' folder something like this template:
<!-- master.blade.php -->
<html>
<head></head>
<body>
<div class="container">
#yield('content')
</div>
</body>
</html>
Then in your every child view, just extend the master view, for example:
<!-- home.blade.php -->
#extends('layouts.master')
#section('body')
<h1>Content</h1>
<p>More content</p>
#stop
So, whenever you'll use something like this:
return View::make('home');
Your child view will extend the master view and content will be yielded inside the div.container.

Categories