
Add class to body in laravel view
Add class to body in laravel view
With this article, we will examine several different instances of how to solve the "Add class to body in laravel view".
There are lots of ways in which you can add class to the body in a laravel view file. It is required if you want to add some properties to a particular page. The Best way you can add class to the body in laravel view is by passing the variable class from child view to layout file while extending the layout.-
Pass variable class from child view to layout
//Extending sidebar layout in child view and passing the variable name to the layout. //resources\views\article.blade.php @extends('layouts.sidebar', ['body_class' => 'article']) //Display variable name to body class in layout. //resources\views\layouts\sidebar.blade.php <body class="{{ $body_class ?? ''}}"> //Or <body class="{{ !empty($body_class) ? $body_class : '' }}">
You can pass variable class name from the child view (article.blade.php) to the sidebar layout and after that, you can display class to your body in the laravel layout file (sidebar.blade.php) which you are extending in the child view file.
-
Add class to body using @yield directive in laravel
//Display class name in laravel layout file. //resources\views\layouts\sidebar.blade.php <body class="@yield('body_class')"> //Pass class name using @section directive in child template. //resources\views\article.blade.php @section('body_class', 'article')
You can add class to the body tag in Laravel view file by passing the class name from the child template (article.blade.php) to the layout template (layouts/sidebar.blade.php) using @section directive and display in layout template using @yield directive.
-
Add class name to body using current route name
//resources\views\<layouts>\<default>.blade.php //Or //resources\views\<index>.blade.php <body class="current_route_name-{{ Route::currentRouteName() }}">
You can pass a class name to the body using current route name in laravel using Route::currentRouteName() in your layout file or child template.
-
Add class to body using url segments in laravel
//resources\views\<layouts>\<default>.blade.php <body class="template-{{ collect(\Request::segments())->implode('-') }}">
-
Add class to body using view composers
//routes\web.php View::composer('*', function ($view) { View::share('viewName', $view->getName()); }); //Or //app\Providers\AppServiceProvider.php //Before class definition use View; //Inside boot method of AppServiceProvider class public function boot() { View::composer('*', function ($view) { View::share('viewName', $view->getName()); }); } //resources\views\<layouts>\<default>.blade.php <body class="template-{{ $viewName }}">
You can add class to the body in laravel view using view composer. View composers are callbacks or class methods that are called when a view is rendered. You can pass class name using View::share() method and display it using {{ $viewName }} in the laravel view file.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- Composer create project laravel/laravel example app
- Multiple Level eager loading in Laravel
- Validation errors for multiple forms on same page Laravel
- Convert multidimensional array to single array in Laravel
- Argument 1 passed to Illuminate\Database\Query\Builder::cleanBindings() must be of the type array, null given
- Check if Relationship Method Exists in Laravel
- How to increment column value of table in Laravel
- The use statement with non-compound name 'DB' has no effect
- Pagination in laravel
- How to get last year records count with month wise in Laravel
- Cast Array to an Object in Controller and then pass to view in laravel
- Credit card validation in laravel
- Argument 1 passed to Symfony\Component\HttpFoundation\Response::setContent() must be of the type string or null, object given
- How to check data inserted or deleted in pivot after toggle method
- How to validate website url in laravel using validaiton
- How to add columns in existing table using migration in laravel
- Retrieve count of nested relationship data in Laravel
- Class 'App\Http\Controllers\User' not found
- How to disable timestamps in laravel
- How to Access Array in blade laravel
- How to authenticate admin users in Laravel ?
- Page loader in laravel
- Use withCount() to get total number of records with relationship
- Laravel append URI in route
- Retain selected value of select box in Laravel