php Laravel 4 - 在视图中包含“部分”视图(不使用 Blade 模板)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17227969/
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
Laravel 4 - including a "partial" view within a view (without using Blade template)
提问by ericbae
In Laravel 3, I used to do this.
在 Laravel 3 中,我曾经这样做过。
<?php render('partials.header'); ?>
This was done in "PHP" views, without using Laravel's Blade templates.
这是在“PHP”视图中完成的,没有使用 Laravel 的 Blade 模板。
What's the equivalent of this in version 4?
版本 4 中的等效项是什么?
I tried
我试过
<?php @include('partials.header'); ?>
This doesn't work.
这不起作用。
If I do
如果我做
@include('partials.header')
I have to save my file as ".blade.php"
我必须将我的文件保存为“.blade.php”
How do I include a "subview" without using the blade template?
如何在不使用刀片模板的情况下包含“子视图”?
回答by Robin Hood
There are different ways to include a view within a view in Laravel 4. Your choice will depend on any one of the outcomes outlined below...
在 Laravel 4 的视图中包含视图有多种不同的方法。您的选择将取决于下面列出的任何一种结果......
For Flexibility
为了灵活性
You can compile (render)the partial views in the appropriate Controller, and pass these views to the Main View using the $data['']
array.
您可以在适当的控制器中编译(渲染)部分视图,并使用$data['']
数组将这些视图传递给主视图。
This may become tedious as the number of views increase, but hey, at least there's a lot of flexibility :)
随着观看次数的增加,这可能会变得乏味,但是嘿,至少有很大的灵活性:)
See the code below for an example:
请参阅下面的代码以获取示例:
Controller
控制器
...
public function showMyView()
{
/* Header partial view */
$data['header'] = View::make('partials.header');
/* Flexible enough for any kind of partial views you require (e.g. a Header Menu partial view) */
$data['header_menu'] = View::make('partials.header_menu');
/* Footer partial view */
$data['footer'] = View::make('partials.footer');
return View::make('myView', $data);
}
...
View
看法
You can include the partials above as follows (at any position in your View code):
您可以按如下方式包含上述部分(在您的视图代码中的任何位置):
<html>
<head></head>
<body>
<!-- include partial views -->
<?php echo ($header) ?>
<?php echo ($header_menu) ?>
<div id="main-content-area"></div>
<?php echo ($footer) ?>
</body>
</html>
Your partial views will now be added to your main View.
您的部分视图现在将添加到您的主视图中。
For Simplicity
为了简单
There's actually a much easier way than using the method above: Simply include this in the html of the view...
实际上有一种比使用上述方法更简单的方法:只需将其包含在视图的 html 中...
View
看法
<html>
<head></head>
<body>
<!-- include partial view: header -->
<?php echo View::make('partials.header') ?>
<div id="main-content-area">
</div>
<!-- include partial view: footer -->
<?php echo View::make('partials.footer') ?>
</body>
</html>
Make sure that the folder structure for the partials is [views/partials/header.php]in order to provide the correct file-path to the View::make() function of Laravel.
确保部分的文件夹结构是[views/partials/header.php],以便为 Laravel 的 View::make() 函数提供正确的文件路径。
WARNING
警告
If you try to pass the $data['page_title']
in a controller, the nested viewswont receive the data.
To pass data to these nested views you need to do it like this:
如果您尝试$data['page_title']
在控制器中传递,嵌套视图将不会接收数据。
要将数据传递给这些嵌套视图,您需要这样做:
<html>
<head></head>
<body>
<?php
/* Pass page title to header partial view */
$data ['page_title'] = "My awesome website";
echo View::make('partials.header', $data);
?>
<div id="main-content-area"></div>
<?php echo View::make('partials.footer') ?>
</body>
</html>
NOTE
笔记
The question clearly stated: "Without using Blade template", so I have made sure to give a solution that does not include any Blade templating code.
该问题明确指出:“不使用 Blade 模板”,因此我确保提供不包含任何 Blade 模板代码的解决方案。
Good luck :)
祝你好运 :)
回答by Mohammad Shoriful Islam Ronju
You can nest your partials in views try this
你可以在视图中嵌套你的部分试试这个
View::make('folder.viewFile')->nest('anyname', 'folder.FileName');
Then access the nested view file from your template {{ $anyname }}
this way you don't have to include files in your view and this should work for .php file also.
然后从您的模板访问嵌套视图文件,{{ $anyname }}
这样您就不必在视图中包含文件,这也适用于 .php 文件。
回答by philcollin_us
I am not sure how many people have been using Laravel 4 in this post, since this post, but if you are looking to include partials or separate your view types you can do it with @includes
我不知道有多少人在这篇文章中使用过 Laravel 4,但如果你想包含部分或分离你的视图类型,你可以使用 @includes
for example, if you want a partials folder for your header, footer, sidebar etc
例如,如果您想要一个用于页眉、页脚、侧边栏等的部分文件夹
create a directory for the partials under
为下的部分创建一个目录
app/views/partials
Then create a partial
然后创建一个部分
app/views/partials/navigation.blade.php
Then in your master template file add the line
然后在您的主模板文件中添加行
@include('partials.navigation')
That is all it takes.
这就是全部。
** Bonus you can also pass data to a partial or include nested partials within a partial
** 奖励您还可以将数据传递给部分或在部分中包含嵌套的部分
回答by Ole
I know this is a bit of a late answer, but I figured since I didn't see this solution amongst the other answers it was ok.
我知道这是一个有点晚的答案,但我想,因为我没有在其他答案中看到这个解决方案,所以没关系。
If you want to include your header and footer on every page I would add them into the before and after filters. Just go to filters.php in your app folder
如果您想在每个页面上包含页眉和页脚,我会将它们添加到前后过滤器中。只需转到您的应用程序文件夹中的 filters.php
App::before(function($request)
{
echo View::make('partials.header');
});
App::after(function($request, $response)
{
echo View::make('partials.footer');
});
When doing it this way you don't need to add anything in the view files to include them.
这样做时,您不需要在视图文件中添加任何内容来包含它们。
回答by Leysam Rosario
You can use View's nest function
你可以使用View的嵌套功能
View::make('default.layout')->nest('header', 'default.header');
Use the third parameter to pass data to the template
使用第三个参数向模板传递数据
View::make('default.layout')->nest('header', 'default.header', ['name' => 'John Doe', 'test' => 'It works!']);
on your views/default/header.blade.php
在您的意见/默认/header.blade.php
<div>hey {{ $name }}! {{ $test }}</div>
回答by farinspace
I am still pretty new to Laravel, but I think the below is pretty ideal ...
我对 Laravel 还是很陌生,但我认为下面的内容非常理想......
Route::get('/', function()
{
$data['title'] = 'sample';
return View::make('page', $data);
});
# /views/partials/header.php
# /views/partials/footer.php
View::composer('page', function($view)
{
$view->with('header', View::make('partials.header', $view->getData()));
$view->with('footer', View::make('partials.footer', $view->getData()));
});
See Laravel View Composers .. http://laravel.com/docs/responses#view-composers
请参阅 Laravel 视图作曲家 .. http://laravel.com/docs/responses#view-composers
/views/page.php
/视图/page.php
<?php echo $header; ?>
<div>CONTENT</div>
<?php echo $footer; ?>
回答by Callum
From within a view, just echo the other view:
从一个视图中,只需回显另一个视图:
echo View::make('header'); //This will look for a view called header.php