简体   繁体   English

是否可以在Laravel(4)刀片模板中获取视图/布局名称

[英]Is it possible to get the view / layout name in a Laravel (4) blade template

I have a layout in which I want to add classes to the body depending on which view is being displayed, ie: 我有一个布局,我想根据显示的视图向主体添加类,即:

<body class="layout-default page-index">

I can do this in Twig quite easily (OctoberCMS uses Twig) but I can't see a way to do it with Laravel's Blade templates (which I prefer anyway). 我可以很容易地在Twig中做到这一点(OctoberCMS使用Twig)但是我看不到使用Laravel的Blade模板(我更喜欢它)的方法。

I'd rather not have to pass a variable to every View::make with the view name as this seems redundant. 我宁愿不必使用视图名称将变量传递给每个View::make ,因为这似乎是多余的。

Good question, very smart way to work with css. 好问题,非常聪明的方式与CSS一起工作。 You would use this typically by adding classes to the body tag, or the main container div. 您通常可以通过向body标记或主容器div添加类来使用它。

within your routes or filters file: 在您的路线或过滤器文件中:

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

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

});

Within your view: 在您的视图中:

<?php echo str_replace('.','-',$view_name);?>

<?php echo str_replace('.','-',Route::currentRouteName());?>

These should get you everything you need. 这些应该可以为您提供所需的一切。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM