How can I get the current view name inside a master layour in Laravel 4?

后端 未结 2 977
忘掉有多难
忘掉有多难 2021-01-19 17:37

in Laravel 4 I have a master blade layout and I want to add a class to the html element like \'tpl-home\', but I need to know which is the current view name called with View

相关标签:
2条回答
  • 2021-01-19 17:58

    Inside your filters file place:

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

    Then in your master blade layout you can access it as $view_name.

    0 讨论(0)
  • 2021-01-19 18:03

    View Composers are a great way to keep/share variables in views, but the names of the views set in routes file are more a fixed value in each view than a variable, that's why, in this case,

    I'd rather use Request::path() == 'viewName' in Blade like this:

    <ul class="nav navbar-nav">
      <li{{ Request::path() == 'admin' ? ' class="active"' : '' }}>
        <a href="{{ URL::to('/admin') }}">Admin</a>
      </li>
      <li{{ Request::path() == 'bookings' ? ' class="active"' : '' }}>
        <a href="{{ URL::to('/bookings') }}">Bookings</a>
      </li>
      ...
    
    0 讨论(0)
提交回复
热议问题