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">
2
  • if you are using dynamic urls like ?action=home or ?action=about then you can use <body class="<?php $_GET['action'] ?>"?
    – Matt
    Commented Apr 28, 2016 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.
    – Mat
    Commented Apr 28, 2016 at 18:31

5 Answers 5

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

2
  • 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!
    – Mat
    Commented Apr 28, 2016 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
    Commented Apr 28, 2016 at 18:49
6

Try this:

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

And on your views

@section('class', 'Your boddy class')
2
  • Where should the second code be put ? I tried before @extends('layouts.app') and after, but doesnt do anything, my body class stays empty :/
    – Dikeneko
    Commented Apr 25, 2017 at 10:14
  • Nice answer!!!!
    – jpussacq
    Commented May 12, 2022 at 20:59
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}}">
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')
1
  • IMHO more flexible with stack/push method Commented Apr 10, 2021 at 19:26
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}}'">

Not the answer you're looking for? Browse other questions tagged or ask your own question.