2013-02-28 100 views
4

我正在使用laravel佈局,我有這樣的設置;Laravel多個嵌套視圖

//控制器

public function action_index() 
{ 
    $this->layout->nest('submodule', 'partials.stuff'); 
    $this->layout->nest('content', 'home.index'); 
} 

//佈局

<!doctype html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Document</title> 
</head> 
<body> 
    @yield('content'); 
</body> 
</html> 

//這是內容模板

@section('content') 
    <div> 
     @yield('submodule') 
    </div> 
@endsection 

我的問題是我怎麼能插入一個局部模板內'內容'部分?我還需要將變量傳遞給第二個模板「submodule」。

$this->layout->nest('partial', 'partials.partial'); 

這不起作用,因爲它將視圖綁定到佈局。而我需要將其綁定到「內容」模板中定義的部分。

任何想法?

回答

0

這裏就是你通常會做的事:

public function action_index() 
{ 
    $this->layout->nest('content', View::make('home.index')->nest('submodule', 'partials.stuff')); 
} 

而且在你的模板:

<!doctype html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Document</title> 
</head> 
<body> 
    {{ $content }} 
</body> 
</html> 

和你home/index.blade.php

<div> 
    {{ $submodule }} 
</div> 
+1

這不起作用 – wnajar 2013-12-05 22:16:32

+0

對我不起作用 – 2015-04-28 22:53:31

5

這裏是我固定的Laravel嵌套視圖問題:

使用此解決方案,您將數據傳遞到您的主視圖以及

解決方案:

你需要渲染partials.stuff您的家庭/ index.blade.php視圖中,然後使視圖呈現在你的template.php '家/ index.blade.php' 的 '內容'

使用<?php render('partials.stuff') ?>

首先請您home/index.blade.php:

<div> 
     <?php render('partials.stuff') ?> 
</div> 

第二渲染你的看法 - 沒有任何嵌套「子模塊」叫

public function action_index() 
{ 
    $this->layout->nest('content', View::make('home.index'),$data) ; 
} 

最後你的模板將保持相同的 - 渲染{{ $content }}

<!doctype html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Document</title> 
</head> 
<body> 
    {{ $content }} 
</body> 
</html> 

希望這可以幫助你,因爲它解決了我問題:)