2013-07-11 65 views
0

我想知道是否可以在Laravel 4中的模板中嵌套模板。我想要一個包含doctype,header和footer等東西的主包裝模板,然後能夠將模板加載到主模板中模板正文部分。Laravel 4中的模板內嵌套模板?

這會給我一個靈活性,讓我的應用程序頁面擁有一個嵌套模板,而不必複製代碼,同時使我能夠將主模板用於非應用程序頁面。

有人可以請提供一個例子,說明如何使用刀片模板引擎完成這項工作嗎?是否有可能從路由器傳入一個值,然後將該值推送到您的嵌套模板?

編輯:

這裏是我的index.blade.php代碼

@extends('layouts.master') 

@section('title') 
    Some page 
@endsection 

@section('content') 
    @include('layouts.app') 
@endsection 
+0

是的,你可以在父視圖中嵌套子視圖,你應該已經閱讀[documentation](http://laravel.com/docs/responses#views) –

+0

我沒看過文檔,這很遺憾沒有回答我的問題。 –

+0

你正在談論子視圖,以及它如何不能解決你的問題 –

回答

2

你可以試試這個多級嵌套

//index.blade.php 
@extends('layouts.master') 

@section('title') 
@parent 
:: new title 
@stop 

@section('content') 
<p>some static contents here</p> 
@stop 

//app.blade.php 
@section('content') 
@parent 
<p>Add here what ever you want to add</p> 
@stop 

now either from your Route or Controller you can nest the index and app, ex- 
return View::make('index')->nest('content','layouts.app'); 

而如果你想傳遞數據以子視圖,你可以通過將數據作爲第三個參數傳遞給nest()

return View::make('index')->nest('content','layouts.app',$data); 
+0

最近的Laravel文檔中不再引用嵌套幫助器。它在你的答案中是否仍然有效? – Atlas

+0

這個答案是針對Laravel 4.我不確定它是否可以用於5。 –