Laravel 4-包括“部分"视图内的视图(不使用Blade模板) [英] Laravel 4 - including a "partial" view within a view (without using Blade template)

查看:119
本文介绍了Laravel 4-包括“部分"视图内的视图(不使用Blade模板)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

第4版中的等效项是什么?

我尝试过

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

这不起作用.

如果我愿意

@include('partials.header')

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

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

解决方案

在Laravel 4的视图中包含视图的方法有多种. >

出于灵活性

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

随着视图数量的增加,这可能变得乏味,但是,至少,它具有很大的灵活性:)

有关示例,请参见下面的代码

控制器

...

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代码中的任何位置)包括:

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

您的局部视图现在将添加到您的主视图中.


为简单起见

实际上有比使用上面的方法简单得多的方法:只需将其包含在视图的html中...

查看

<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()函数提供正确的文件路径.

警告

如果尝试在控制器中传递$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>

注意

该问题明确指出:不使用Blade模板",因此我确保提供不包含任何Blade模板代码的解决方案.

祝你好运:)

In Laravel 3, I used to do this.

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

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

What's the equivalent of this in version 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"

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

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.

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

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.

WARNING

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>

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.

Good luck :)

这篇关于Laravel 4-包括“部分"视图内的视图(不使用Blade模板)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆