Laravel 4 - 包括一个“部分"视图中的视图(不使用 Blade 模板)

2022-01-08 00:00:00 php laravel laravel-4

在 Laravel 3 中,我曾经这样做过.

In Laravel 3, I used to do this.

<?php render('partials.header'); ?>

这是在PHP"视图中完成的,没有使用 Laravel 的 Blade 模板.

This was done in "PHP" views, without using Laravel's Blade templates.

在版本 4 中相当于什么?

What's the equivalent of this in version 4?

我试过了

<?php @include('partials.header'); ?>

这不起作用.

如果我这样做

@include('partials.header')

我必须将我的文件保存为.blade.php"

I have to save my file as ".blade.php"

如何在不使用刀片模板的情况下包含子视图"?

How do I include a "subview" without using the blade template?

推荐答案

在 Laravel 4 中,有多种方法可以在视图中包含视图.您的选择将取决于下面列出的任何一种结果...

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

您可以在适当的控制器中编译(渲染)部分视图,并使用 $data[''] 数组将这些视图传递给主视图.

You can compile (render) the partial views in the appropriate Controller, and pass these views to the Main View using the $data[''] array.

随着观看次数的增加,这可能会变得乏味,但是嘿,至少有很大的灵活性:)

This may become tedious as the number of views increase, but hey, at least there's a lot of flexibility :)

示例见下面的代码:

...

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.

其实有一个比使用上面的方法更简单的方法:只需在视图的 html 中包含这个...

There's actually a much easier way than using the method above: Simply include this in the html of the 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>

确保部分的文件夹结构是 [views/partials/header.php] 以便为 Laravel 的 View::make() 函数提供正确的文件路径.

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.

如果您尝试在控制器中传递 $data['page_title'],嵌套视图将不会接收数据.
要将数据传递给这些嵌套视图,您需要这样做:

If you try to pass the $data['page_title'] in a controller, the nested views wont receive the data.
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>

注意

问题明确指出:不使用 Blade 模板",所以我确保给出一个不包含任何 Blade 模板代码的解决方案.

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.

相关文章