Include template is not showing content when page called? - php

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

Related

Laravel yield is empty

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');
});

Laravel 8: How to IGNORE a file which is imported to app.blade.php

The body of my app.blade.php contains this:
<body>
#include('layouts.navbar')
<section class="container">
#yield('content')
</section>
#include('layouts.footer')
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.bundle.js"></script>
</body>
And I extends this at all my blades, but I want the landing page of website to ignore footer.blade.php from app.blade.php which is this one:
#include('layouts.footer')
So how can I do that? Should I just manually include the footer directly at the blades or there is a way to ignore one of the included files of app.blade.php ?
Easily you can make 2 layout. Like one is app.blade.php another one is app_no_footer.blade.php. Ofc second one does not include layouts.footer
You can pass variable to layout when you extend
#extends("app", [
"footer" => false
])
Like this.
On your app.blade.php you check like:
#if(isset($footer) && $footer)
#include("layouts.footer")
#endif

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 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

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