簡體   English   中英

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

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

在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模板代碼的解決方案。

祝好運 :)

您可以將局部視圖嵌套在視圖中

View::make('folder.viewFile')->nest('anyname', 'folder.FileName');

然后以這種方式從模板{{ $anyname }}訪問嵌套視圖文件,而不必在視圖中包括文件,這也適用於.php文件。

自從發布以來,我不確定在這篇文章中有多少人使用過Laravel 4,但是如果您希望包含局部視圖或分離視圖類型,可以使用@includes來完成。

例如,如果您想為頁眉,頁腳,側邊欄等添加局部文件夾

在下面創建局部目錄

app/views/partials

然后創建一個局部

app/views/partials/navigation.blade.php

然后在主模板文件中添加以下行

@include('partials.navigation')

這就是全部。

**獎金,您還可以將數據傳遞到部分或在部分中包含嵌套的部分

我知道這是一個較晚的答案,但我認為,因為在其他答案中我看不到這種解決方案是可以的。

如果要在每個頁面上都包含頁眉和頁腳,則可以將它們添加到before和after過濾器中。 只需轉到您應用程序文件夾中的filters.php

App::before(function($request)
{
    echo View::make('partials.header');
});

App::after(function($request, $response)
{
    echo View::make('partials.footer');
});

這樣一來,您無需在視圖文件中添加任何內容即可將其包括在內。

您可以使用View的nest函數

View::make('default.layout')->nest('header', 'default.header');

使用第三個參數將數據傳遞到模板

View::make('default.layout')->nest('header', 'default.header', ['name' => 'John Doe', 'test' => 'It works!']);

在您的意見/ default / header.blade.php上

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

我對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()));
});

參見Laravel View Composers .. http://laravel.com/docs/responses#view-composers

/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