2

I've tried a variety of ways, including the popular post here: Add View Name to CSS Class on Body

But it pulls up the master template, rather than the sub-template name. I use @yield and @extends and such. So at the moment this is what a part of my file system looks like in my views:

-Views
--Layouts
---=master.blade.php
--Shipments
---=index.blade.php

So my shipments.index file will @extends('layouts.master'), just so I don't have so much redundancy. However, referencing the above post, I get the following result:

<div id="page" class="layouts.master">

which is not what I'd like, I'd prefer at the very least "shipments.index", I don't mind if it's longer, I just want it identified.

halfer
  • 19,824
  • 17
  • 99
  • 186
Matthew
  • 1,565
  • 6
  • 32
  • 56
  • Look into something like [this](https://stackoverflow.com/a/46839273/3226121). It's used to get the view directory, but with a bit of modification it could work for you – ljubadr Oct 20 '17 at 16:06
  • @ljubadr - what would I change in it? Using it outright just gives me a blank response in the html. No errors or anything, just nothing responding using the {{ $view_folder }} - do you have any suggestions? Thanks! – Matthew Oct 20 '17 at 22:08
  • I've created an answer, take a look and let me know if it works – ljubadr Oct 20 '17 at 22:20
  • Possible duplicate of [Laravel 5 get view name](https://stackoverflow.com/questions/29458845/laravel-5-get-view-name) – ljubadr Oct 20 '17 at 22:32

1 Answers1

6

You can use Laravel View Composers

View composers are callbacks or class methods that are called when a view is rendered. If you have data that you want to be bound to a view each time that view is rendered, a view composer can help you organize that logic into a single location.

In the file app/Providers/AppServiceProvider.php, edit the public function boot() method

/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot(Application $app)
{
    view()->composer('*', function($view) {
        view()->share('view_name', $view->getName());
    });
}

After this, in any blade template you will now have access to a variable

{{ $view_name }}

ljubadr
  • 2,174
  • 1
  • 20
  • 24