How to using extending the same masterpage layout in two partials? - php

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!

Related

Laravel 8 – Blade template – read variable from another template file

This must be really simple, but since I’m new to Laravel, I’m having difficulties on finding this feature on documentation or in stackoverflow.
I’m going to try to build a very simple example to try to show what I want.
Let’s say I have a file called data.blade.php
#php
$myVariable = 'xyz';
#endphp
Then, I have my layout template file, called: home.blade.php
#extends('data.blade')
#section('content')
#php
echo $myVariable;
#endphp
#endsection
When I try to do this, it returns me an error:
Undefined variable $myVariable (etc)
How can I use a variable defined in another blade file? I used extends to exemplify, but I´m not sure if that is the right architecture.
Can anyone point me to the right way of accomplishing this?
Thanks!
you are extending data.blade.php and this file need to have some element to serve as template and be extended.
You can take any html5 file and rename this as template.blade.php, but where you want to render a section in your template must have a yield('section-name').
Make a new app.blade.php should look like this
<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>
Then, in your home.blade.php
#extends('layouts.app')
#section('title', 'Page Title')
#section('sidebar')
#parent
<p>This is appended to the master sidebar.</p>
#endsection
#section('content')
#include(data)
{{$myVariable}}
<p>This is my body content.</p>
#endsection
From Laravel Doc

Different headers in different views (Blade template, Laravel)

I use the Bladetemplate of Laravel. Is there a way to set different header for different views with only one include in master.blade.php?
master.blade.php
#include("elements.header")
#yield('content')
#section("footer")
#show
view.blade.php
#extends("layouts.master")
#section("title")
#stop
#section("content")
#include("elements.error")
#section("footer")
#include("elements.footer")
#stop
If you want to include different header templates for different views, there is no need to include anything from your layout. Instead, include the proper header template into a separate section in your views and then display that section in the master template:
master.blade.php
#yield('header')
#yield('content')
viewA.blade.php
#extends("layouts.master")
#section('header')
#include('headerA')
#stop
#section('content')
view content
#stop
viewB.blade.php
#extends("layouts.master")
#section('header')
#include('headerB')
#stop
#section('content')
view content
#stop
This way, each of your views includes different header templates into the header section that will be later displayed in master layout with #yield('header').
In your main layout
<title>#yield('title','Home')</title>
Then In your views Just call
#section('title','My View 1')
#section('title','My View 2')
The second parameter in yield is the default if none defined.

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

Fetch data from inside Blade view

I'm learning about Laravel. I know how to create a controller that builds an array of data from an Eloquent model, pass that array to a view and display it.
What I am trying to do is create a main view that has the header and footer information all pages use. Then I try to use a child view to display the body.
The main blade file has an include for a file that builds a dynamic navigation bar.
How can I go about retrieving data from within the include file the main blade file that's called by a child?
What I have (very abbreviated) is:
main.blade.php
<html>
#include('header')
<body>
#section('content')
#stop
</body>
</html>
home.blade.php
#extends('main')
#section('content')
<h1>Home</h1>
#endsection
Now from the controller, I call the home.blade.php view
Can I fetch the data thats needed in the header view from INSIDE the header file rather than having to pass the data from every view I call? Or do I need to simply create a global function that I call in every controller before I call a view?
In your main page:
<html>
#include('header')
<body>
#yield('content')
</body>
</html>
In your view:
#extends('main')
#section('content')
<h1>Home</h1>
#endsection
The dynamic navigation bar you mention should come from a view composer. In this way it will be available all the time to all views.
Do a test with the following in your routes.php file:
View::composer('*', function($view)
{
// $navBar stuff
$view->with('navBar', $navBar);
});
Now $navBar will be available in your views.
http://laravel.com/docs/5.1/views#view-composers

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