Not having to extend and section in every file Laravel/Blade - php

Is it possible to make every view extend my main layout without having to put #extends('layout') and #section('content') in every file at the top? For example by using a provider? I'm new to Laravel and Blade.

Actually its better to use extend but in case you want to get rid of #extends or #section, you can always return your main layout from controller and pass views as parameters to the main layout.
mainlayout.blade.php:
//replace yield directive with content variable
// #yield('content')
{!!$content!!}
in controller:
//return view('test');
$content=view('test')->render();
return view('mainlayout',['content'=>$content]);
Now in test view you don't need #extends and #section.

Related

Header Footer Controller in Laravel

I am working on Laravel for the first time
i have to make a Front End Menu Dynamic in Header and Footer [ list of categories will come from database ]. which controller I have to use for this.?
any common controller available in this framework to send data to header and footer.
When I receive the data in HomeController index Action its available for the home page only.
class HomeController {
public function index()
{
$categories = Category::get();
return view('home', compact('categories'));
}
}
Thanks.
This is a perfect case for View Composers:
View composers are callbacks or class methods that are called when a view is rendered. If you have data that you want to be bound to a view each time that view is rendered, a view composer can help you organize that logic into a single location.
You may attach a view composer to multiple views at once by passing an array of views as the first argument to the composer method:
View::composer(['partials.header', 'partials.footer'], function ($view) {
$view->with('categories', [1, 2, 3]); // bind data to view
});
Now you could simply retrieve $categories within your view (blade template).
Tip: Common practice is to create a new service provider called ComposerServiceProvider and place the abovementioned composer logic in the boot() method.
I assume you are using the Header and Footer in a master layout file. In this case you need to send all header/footer info every request. Which would be silly so instead use View Composers:
Define them in your appServiceProvider in the boot() method
view()->composer('home', function($view) {
$view->with('categories', App\Category::all());
});
In my example i made 'home' the name of the view, since it is in your example. But i would make a master file called layout and include a header and footer partial. If you want categories inside your header you could make a view()->composer('layout.header') with only header data.
which controller I have to use for this.?
Any
any common controller available in this framework to send data to header and footer
No.
You control what is returned from the Controller to be the response. You can design layouts and break them up into sections and have views that extend them. If you setup a layout that has the header and footer and your views extend from it, you can use a View Composer to pass data to that layout when it is rendered so you don't have to do this every time you need to return a view yourself.
Laravel Docs 5.5 - Front End - Blade
Laravel Docs 5.5 - Views - View Composers
In case of this u can use components
https://laravel.com/docs/8.x/blade#components
php artisan make:component Header
View/Component.Header.php
public function render()
{
$category = [Waht you want];
return view('views.header', ['category'=>$category]);
}
Then include Header.php to your blade like this
views/front.blade.php
<html>
<body>
<x-header/> <!-- like this -->
#yield('content')
#include('footer')
<body>

Share data from a controller to partial footer

In my app, i divided the folders in my view by:
"posts"
"pages"
"partials"
layout.blade.php
In my folder partials i have blade files, like nav.blade.php and footer.blade.php. In my folder "pages", i have files like home.blade.php and some other internal pages.
But now i need to pass a viable from a controller to my footer, since is needed to be shared in all views.
How i pass the variable from my PagesController that manages blade files inside the folder "pages" in the footer?
Example routeS:
Route::get('/', 'PageController#getHome');
Route::get('/registar', 'AuthenticationController#getRegistration');
Route::post('/post/comment', 'CommentController#store');
Route::get('/login', 'AuthenticationController#getLogin');
If i understood correctly, you want to share some data every time particular view is called? If that so, you can do this:
/* App\Providers\AppServiceProvider.php
** Edit the boot function, (dont forget to include View facade) */
public function boot(){
View::composer('path_to_view', function($view)
{
$view->with('variable', $variable); // you can pass array here aswell
});
}
Other way to do this (if you are extending the footer layout from the view you are accessing), is to pass data when you extend the layout, like this:
#extends('footer', ['data' => $data])
If you need to share same variable in multipe view from providers. you can also try to this
public function boot()
{
//set view path in array
View::composer(['path_to_view1', 'path_to_view2'], function ($view) {
$view->with('variableName', $variableName);
});
}

Is it possible to get the view / layout name in a Laravel (4) blade template

I have a layout in which I want to add classes to the body depending on which view is being displayed, i.e.:
<body class="layout-default page-index">
I can do this in Twig quite easily (OctoberCMS uses Twig) but I can't see a way to do it with Laravel's Blade templates (which I prefer anyway).
I'd rather not have to pass a variable to every View::make with the view name as this seems redundant.
Good question, very smart way to work with css.
You would use this typically by adding classes to the body tag, or the main container div.
within your routes or filters file:
View::composer('*', function($view){
View::share('view_name', $view->getName());
});
Within your view:
<?php echo str_replace('.','-',$view_name);?>
<?php echo str_replace('.','-',Route::currentRouteName());?>
These should get you everything you need.

Override section in a laravel blade template throwing undefined variable errors

I am using Laravel 4 and blade templates, and running into an issue when I try and extend a template.
In my layout I have
#yield('content')
and in my page I have
#section('content')
Welcome {{ $name }}
#stop
which works fine, I've created another page very similar to my first, and just want to change override the admin content section. The other sections in the template are fine.
so I extend my page, and do
#section('content')
Hello!
#stop
I get an undefined notice with the $name variable.
I tried
#section('content')
Hello!
#overwrite
and same deal, I get the notice error.
I checked my controller and it IS using the correct template. I am not calling #parent so I don't understand, how can I overwrite a section in a template with out notice errors?
Blade layouts work their way up the tree to the route or master view before rendering any of the children. Thus, nested views that extend others must always have their parent rendered before they are. As a result, parent views that contain sections will always be rendered prior to the child.
To overcome the problem you are experiencing it is a good idea to only ever nest pages that don't overwrite parents sections that contain variables, as the parents content will always be rendered before the child.
As the above ideal can't always be adhered to or a parents section content is still required, a good alternative method would be to use view composers. View composers give you an opportunity to declare variables for any specific view whenever they are rendered.
View::composer(array('pages.admin'), function($view)
{
$view->with('name', Auth::user()->username);
});
Another alternative would be to use a view creator. Creators are fired the moment a view is instantiated rather than rendered. This method allows you to overwrite the variable should you so wish prior to the view being rendered.
View::creator(array('pages.admin'), function($view)
{
$view->with('name', Auth::user()->username);
});
You can read up more about these methods in the documentation here. (Or here for the Laravel 5 documentation.)
I can't guarantee support for Laravel 4 but for those looking for a solution that works in Laravel 5.5 (and probably a fair bit further back – hard to check) is to define the variables you need when #extending.
E.g. in the example in the question:
#extend('my.parent.view', ['name' => ''])
This approach can be especially useful if the data needed is available to the child-view, but under a different name.
E.g. if a parent-view needed a $parent variable, but the child view only has a $child variable which has a property referencing the parent, you might do:
#extend('my.parent.view', ['parent' => $child->parent])
I am not sure if this is a bug or intentional, but it seems like Laravel renders the variables before interpreting blade instructions.
The workaround would be this:
views/layouts/testlayout.blade.php:
<html>
<body>
#yield('sidebar', 'This is the master sidebar. {{ $name }}' )
<div class="container">
#yield('content')
</div>
</body>
</html>
actual view: views/test.blade.php
#extends('layouts.testlayout')
#section('sidebar')
No variable in the child
#stop
#section('content')
This is a child content
#stop
This prevents the variable $name to get rendered if there is a section with that name in the actual view. It seems like this is the approach if the content in the layout file contains a variable
In your page try this :
#section('content')
#hasSection('content')
#yield('content')
#else
Welcome {{ $name }}
#endif
#stop
And read this post for more inspiration https://laracasts.com/discuss/channels/laravel/laravel-blade-layouts-inheritence-problem
I found an easy solution for this problem :)
Just add the # symbol before the variable/method you call in the parent view. If there is an error (notice/warning) PHP will ignore that and continue the execution.
It's not perfect, but save us to code view/composers.
Hope this help!

How do i load a method in a master blade template?

i am using laravel's blade template and i have a master template for all my pages. In the master template i have a top bar and a sidebar. I want to load something in the sidebar. But i don't know how do it in a simpler way. Now i am calling that method (which i want in to display in my sidebar) in every controller i have like this:
View::make()->with('data_to_load_in_sidebar',$data_to_load_in_sidebar)
How can i load this only once, not every time i generate a view?
This is what view composers are for, any view that is loaded will automatically have it's composer run alongside providing the view with any extra data it may require.
View::composer(array('partials.sidebar'), function($view)
{
$news = News::all();
$view->with('news', $news);
});
I typically put this in my routes.php file in both L3 and L4.
In the view views\partials\sidebar.blade.php you now always have access to the variable $news that will contain all models from the News collection.
I would share top bar & sidebar data in constructor (prefferably in some BaseController's contructor, that other controllers extends).
public function __construct()
{
// if needed, call parent's contructor method as well
parent::__construct()
$data_to_load_in_sidebar = loadDataForSidebar();
View::share('data_to_load_in_sidebar',$data_to_load_in_sidebar)
}

Categories