Matty Clarke Matty Clarke - 9 months ago 32
PHP Question

Add View Name to CSS Class on Body

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

Any help is greatly appreciated.


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