Matty Clarke Matty Clarke - 2 years ago 92
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.

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download