6

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">
Shaz
  • 2,647
  • 2
  • 35
  • 45
Matt
  • 484
  • 6
  • 19
  • if you are using dynamic urls like ?action=home or ?action=about then you can use – Matt Apr 28 '16 at 18:29
  • @Matt Hi Matt, I'm using Laravel so I'm assuming that I'd need to make a controller for capturing the view name then convert it to a variable. Once it's in a variable, I'd need to echo it to the class like {{ $viewname }} or something along these lines. It's just getting to this point that I'm confused about. – Matt Apr 28 '16 at 18:31

5 Answers5

9

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

Stuart
  • 6,630
  • 2
  • 24
  • 40
  • Yeah it's spot on! So easy to knock out applications once you get used to the system. The blade template system just makes it too easy haha! – Matt Apr 28 '16 at 18:47
  • Indeed, it's a pleasure to use, and like you say - one can knock up a lovely looking and functional PoC / prototype in no time. – Stuart Apr 28 '16 at 18:49
6

Try this:

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

And on your views

@section('class', 'Your boddy class')
Alex Munoz
  • 155
  • 1
  • 8
5

Use this in your controller

View::composer('*', function($view){

    View::share('view_name', $view->getName());

});

Use this in your view page

<body class="{{$view_name}}">
Shyam Naru
  • 305
  • 1
  • 6
5

Open your AppServiceProvider class, boot() method and insert this code:

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

Then you can access $view_name variable in your view.

Regarding @Stuart answer, use $view->getName() will returns exactly view path with dot notation. For example, you view is under resources/views/settings/index directory, your view name is: settings.index.

So then? the class name will contains dot character means that is little more confusing whilst writing CSS. See this thread: Styling elements with a dot (.) in the class name

By replacing . with space, the class name in above example will becomes settings index and you can use .settings selector, or .index selector, or .setting.index selector.

Custom Class Name

The above method is for automatically generate class name, you have many methods:

@yield() and @section()

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

Then in your views:

@section('body-class', 'your-class-name')

@stack() and @push()

Since Laravel 5.4, you can use @stack and @push, this allows you push as many time as you want.

<body class="@stack('body-class')">

Then in your views

@push('body-class', 'your-class-name')
@push('body-class', ' another-class-name')
Community
  • 1
  • 1
Tan Nguyen
  • 1,636
  • 1
  • 12
  • 9
1

You can easily set in your controller (in contructor or in action's method) class name with:

View::share('bodyClass', 'nameOfCertainClass');

and then:

<body class="{{$bodyClass or 'default}}'">

Filip Koblański
  • 9,718
  • 4
  • 31
  • 36