Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add View Name to CSS Class on Body

Tags:

php

laravel

I'm looking for a way to echo out the view name and place it in to the master template inside the body tag as a CSS Class.

The reason for this is I have some pages where certain CSS Elements need to change however inline code is inefficient and I don't really want to make a dedicated view for that one page.

So, for example, if I was to land on the home page the body tag would be:

<body class="home">

Where if I was to go to about the about us page, it would change to:

<body class="about">
like image 840
Matt Avatar asked Nov 29 '22 14:11

Matt


2 Answers

Add to filters (or routes):

View::composer('*', function($view){
    View::share('view_name', $view->getName());
}); 

You can then access the view name using $view_name

like image 140
Stuart Avatar answered Dec 05 '22 22:12

Stuart


Try this:

<body class="@yield('class')">

And on your views

@section('class', 'Your boddy class')
like image 27
Alex Munoz Avatar answered Dec 06 '22 00:12

Alex Munoz