I have a sidebar include file that is present on every page of the website I am working on.
layout.blade:
<div class="after-login buying-process-wrapper">
<!--page content-->
{{ $content }}
<!-- end of page content-->
<!-- Sidebar -->
#include('layout.sidebar')
<!-- End of Sidebar -->
</div>
My controller sets $layout and renders the above blade file, but layout.sidebar is an include file which requires PHP to populate it's content.
Can I set a controller/route for this sidebar alone (and how would I do it?), or am I forced to have to duplicate the same calls to the function that handles the sidebar content in every controller?
I'm trying to find a better solution than having to go in to every controller and calling ->sidebar() every time each page is loaded.
Thanks
Why are you still using Laravel 4.2? It't 5.7 now and I strongly recommend you to upgrade to the latest version.
Answer to your question:
No, you don't need to call functions that handle your sidebar contents in every controller. You can share you common data across all or some of your views by using View Composer.
From Laravel 4.2 Documentation:
View composers are callbacks or class methods that are called when a view is rendered. If you have data that you want bound to a given view each time that view is rendered throughout your application, a view composer can organize that code into a single location. Therefore, view composers may function like "view models" or "presenters".
If you are using newer versions of Laravel, remember to switch to the right documentation from the right upper corner.
Also, when using Blade #include directive, you can optionally pass variables into the "included" components:
#include('layout.sidebar', ['my_var' => 'value goes here'])
And you can use {{ $my_var }} in your component just like you normally would in your blade templates.
I recently started to build a Laravel/blade web application and I want to switch between what my layout view is showing. I want to replace 'content' with some other blade.php view when I press a button in the layout file. For example in ReactJS you can just determine the rendered content with an IF statement and some vars.
<div class="container">
#yield ('content')
</div>
I googled a bit but couldn't find a straight forward solution so I wondered if this is common in Laravel or do you just have to make a lot of different layout files with other #yield('...')? A lot of code would be duplicated right?
You can use conditional blade directives
#if(Session::get(user_type') == 'Admin')
#extends('layouts.admin')
#else
#extends('layouts.normal')
#endif
#section('title')
#endsection
#section('content')
etc ....
I am facing a pretty strange problem working with templates in Laravel. I will demonstrate it with the following example.
css/custom.css
.muted{ color:red }
Layout.blade.php
<html>
<head>
<title> HomePage </title>
<link href="css/custom.css" rel="stylesheet" />
</head>
<body>
<p>Layout file</p>
#yield('content')
</body>
</html>
subview1.blade.php
#extends('Layout')
#section('content')
<p class="muted"> From sub view 1 </p>
#stop
subview2.blade.php
#extends('Layout')
#section('content')
<p class="muted"> From subview 2 </p>
#stop
Actually what is happening is that for subview 2 the class muted is not applied. And console shows that it could not load the resource i.e custom/css when second view is loaded but works fine with subview1
Any pointers to the real issue and solution are highly appreciated.
EDIT
Attached is the directory structure. And I am referencing it like this.
1) dashboard_layout.blade.php (Layout File)
2) dashboard_index.blade.php ( subview1 )
3) course_index.blade.php (subview2)
dashboard_index.blade.php
#extends('teacher.dashboard.dashboard_layout')
course_index.blade.php
#extends('teacher.dashboard.dashboard_layout')
Actually, It's always confusing to reference the same assets using relative paths.You have to have an eagle eye to work out.Here comes Laravel's Html providers. Use them to reference your assets in your blade files as mentioned below rather than using simple link and script tags.
Html::style('assets/css/custom.css') // For style sheets.
Html::script('assets/js/custom.js') // For scritps.
NOTE : assets directory mentioned above is under your public folder as per Laravels app structure.
Html & Form Providers were shipped with Laravel basic app in previous versions.But in latest versions of Laravel. They are removed from the core.So above code snippet will produce an error. You can follow the minimal steps mentioned here laravelcollective to fix the issue.
Happy Learning !
I have tried all in this link :
php laravel blade template not rendering
My codes are here.
my views/layouts/master.blade.php file contains
<html>
<body>
#section('sidebar')
This is the master sidebar.
#show
<div class="container">
#yield('content')
</div>
</body>
</html>
my views/child.blade.php file contains
#extends('layouts.master')
#section('content')
<p>This is my body content.</p>
#endsection
my route.php contains
Route::get('blade', function () {
return view('child');
});
from my browser I typed in :
http://localhost/larablog/public/blade
The output is a blank page.
I tried with non blade pages and it worked, I also passed normal variable to non blade pages which also worked. When I try with blade templating, the page seems to be blank always.
Please let me know where I went wrong.
Thanks.
As in the following link
Proper laravel storage permissions
I had a permission issue at
storage/framework/views folder.
I changed the folder permission to 777 and it all solved the issue.
May be Problem is with the routing
Route::get('/blade', function () {
return view:('child');
});
If you're running on apache make sure the web server has write permission to the storage directory.
I tried to use the laravel's template system: blade but seems like not working when using the code below in the file users.blade.php:
#extends('layout')
#section('content')
Users! #stop
and browser,
#extends('layout')
That should work if you have a template file at /app/views/layout.blade.php that contains
<p>Some content here</p>
#yield('content')
<p>Some additional content here</p>
Then in your /app/views/user.blade.php, the content
#extends('layout')
#section('content')
<p>This is the user content</p>
#stop
If you call return View::make('user') you should have the compiled content
<p>Some content here</p>
<p>This is the user content</p>
<p>Some additional content here</p>
I hope that helps clarify things for you. If not, can you provide your template file locations and the relevant content?
Just remove the extra space or anything before #extends('yourlayoutfile').
It should be the first thing to be rendered in the file.
I was facing the same problem and tried many things.Suddenly I found a single space at the starting of the file before #extends.
Removed the space and is working fine.
Thanks.
Format:
#extends('layouts.default')
#section('content')
.....
#stop
---Edit----
If this didnt work then try :
Copy all the content in the file and then delete the file.
Create a new file and save it as filename.blade.php
Only after saving the file paste the content into the page.
Save the changes and run it.
This works.
Thank you.
Where is your layout?
If its in app/views/layouts, then it should be
#extends('layouts.index')
(assuming the name is index.blade.php)
ex: #extends('layouts.foo') equals a file in app/views/layouts/ called either foo.blade.php or foo.php. (depending if you use blade)
I have the same problem. What is did is:
1. in routes.php
Route::get('about', 'AboutController#index');
that
AboutController is a controller file AboutController.php in app/controllers
index is a function inside that controller.
2.Create AboutController.php in app/controllers
class class AboutController extends BaseController {
protected $layout = 'layouts.default';
$this->layout->content = View::make('pages.about');
}
You can look at this reference: Defining A Layout On A Controller
By default,Laravel has a layouts folder inside views folder, i.e. app/views/layouts and in this folder you keep your layout files, i.e. app/views/layouts/index.master.php and if you have something similar then you should use something like this:
#extends('layouts.master')
#section('content')
<p>Page Content</p>
#stop
This will inherit/use the master.blade.php file (as layout) from layouts folder, here, layouts.master means layouts/master.blade.php.
In your master.blade.php file you mast have this
#yield('content')
So, data/content from the view between #section('content') and #stop will be dumped in the place of #yield('content') of your layout.
You can use any name for your layout file, if it's layouts/main.blade.php then you should use
#extends('layouts.main')
Make sure you inserted the css link in App.blade.php
For me By default there is no link to the css file
Insert the following link in app.blade.php
<link rel="stylesheet" href= "/css/app.css" >
now its works fine :)
list things to make sure
file name and path properly given
double-check .blade.php file extention
layouts.admin.blade.php
<section class="content" style="padding-top: 20px">
#yield('content')
</section>
#extends('layouts.admin')
#section('content')
<p> this is Order index view</p>
#endsection
Try this!
php artisan cache:clear
php artisan view:clear
php artisan route:clear
php artisan clear-compiled
let's say you have 'master.blade.php' and 'index.blade.php'.
and both of files are in views->home directory. when you want to use #extends in 'index.blade.php' by calling 'master.blad.php' , you should write in index.blade.php file this statment:
#extends('home.master')
not
#extends('master')
Simply save your source using encoding UTF-8 without signature.