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

╄→尐↘猪︶ㄣ 提交于 2020-01-11 09:55:11

问题


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::make.

<!doctype html>
<html lang="es" class="tpl-{{Str::slug($currentViewName)}}">

Does Laravel provide any function for retrieving this?

Thanks


回答1:


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.




回答2:


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>
  ...


来源:https://stackoverflow.com/questions/22403655/how-can-i-get-the-current-view-name-inside-a-master-layour-in-laravel-4

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!