Laravel - 页眉/页脚包含在视图中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16885083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:51:59  来源:igfitidea点击:

Laravel - header/footer includes in views?

laravellaravel-4

提问by user1797484

I am new to Laravel, I would like to create my layout without using blade.

我是 Laravel 的新手,我想在不使用刀片的情况下创建布局。

I have created a header.php view and a footer.php view.

我创建了一个 header.php 视图和一个 footer.php 视图。

In the filters.php file, I did this:

在filters.php文件中,我这样做了:

   App::before(function($request)
{
    return View::make('layout/top');
    //
});


App::after(function($request, $response)
{
    return View::make('layout/bot');
    //
});

And in my routes:

在我的路线中:

Route::get('/', function()
{
    return View::make('hello');
});

The header displays fine...but not the hello view or footer view.

页眉显示正常...但不是 hello 视图或页脚视图。

What am I doing wrong?

我究竟做错了什么?

回答by Kevin Crawley

Consider rendering your header and footer views to a variable, and then passing that to your content view. This also allows you to pass in extra data such as meta, js, styles, etc. that may be unique to the page your delivering to the DOM.

考虑将页眉和页脚视图渲染为变量,然后将其传递给内容视图。这还允许您传入额外的数据,例如元、js、样式等,这些数据可能是您交付给 DOM 的页面所独有的。

$data['header'] = View::make('templates/header')->render();
$data['footer'] = View::make('templates/footer')->render();
return View::make('myview', $data);

回答by Guy Incognito

I believe App::aftergets fired after the request: application-events

我相信App::after在请求后会被解雇:application-events

Myself, I use a single template (blade) which has a placeholder for content - You can use standard php in a blade template and this seams to give me more flexibility than controller layouts: templating

我自己,我使用单个模板(刀片),它有一个内容占位符 - 您可以在刀片模板中使用标准 php,这个接缝比控制器布局给了我更多的灵活性:模板