简体   繁体   English

Laravel 4-在视图中包含“部分”视图(不使用Blade模板)

[英]Laravel 4 - including a “partial” view within a view (without using Blade template)

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? 如何在不使用刀片模板的情况下包含“子视图”?

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. 您可以在适当的Controller中编译(渲染)局部视图,并使用$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): 您可以按如下方式包括上述部分内容(在View代码中的任何位置):

<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 views wont receive the data. 如果尝试在控制器中传递$data['page_title'] ,则嵌套视图将不会接收数据。
To pass data to these nested views you need to do it like this: 要将数据传递到这些嵌套视图,您需要这样做:

<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 :) 祝好运 :)

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文件。

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 **奖金,您还可以将数据传递到部分或在部分中包含嵌套的部分

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. 如果要在每个页面上都包含页眉和页脚,则可以将它们添加到before和after过滤器中。 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. 这样一来,您无需在视图文件中添加任何内容即可将其包括在内。

You can use View's nest function 您可以使用View的nest函数

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 在您的意见/ default / header.blade.php上

<div>hey {{ $name }}! {{ $test }}</div>

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 View Composers .. http://laravel.com/docs/responses#view-composers

/views/page.php /views/page.php

<?php echo $header; ?>
<div>CONTENT</div>
<?php echo $footer; ?>

从一个视图中,只需回显另一个视图:

echo View::make('header'); //This will look for a view called header.php

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

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