標籤:style blog http color 使用 io strong 檔案
控制器布局
在Laravel架構中使用模板的一種方法就是通過控制器布局。通過在控制器中指定 layout 屬性,對應的視圖會被建立並且作為請求的預設返回資料。
在控制器中定義一個布局
class UserController extends BaseController { /** * The layout that should be used for responses. */ protected $layout = ‘layouts.master‘; /** * Show the user profile. */ public function showProfile() { $this->layout->content = View::make(‘user.profile‘); }}Blade模板
Blade是Laravel架構下的一種簡單又強大的模板引擎。 不同於控制器布局,Blade模板引擎由模板繼承和模板片段驅動。所有的Blade模板檔案必須使用Blade .blade.php 副檔名。
定義一個Blade布局
<!-- Stored in app/views/layouts/master.blade.php --><html> <body> @section(‘sidebar‘) This is the master sidebar. @show <div class="container"> @yield(‘content‘) </div> </body></html>
使用一個Blade布局 (檔案名稱.blade.php)
@extends(‘layouts.master‘)@section(‘sidebar‘) @parent <p>This is appended to the master sidebar.</p>@stop@section(‘content‘) <p>This is my body content.</p>@stop
(渲染時直接接檔案名稱,不要blade)
注意一個Blade布局的擴充視圖簡單地在布局中替換了模板片段。通過在模板片段中使用 @parent 指令,布局的內容可以被包含在一個子視圖中,這樣你就可以在布局片段中添加諸如側邊欄、底部資訊的內容。
Sometimes, such as when you are not sure if a section has been defined, you may wish to pass a default value to the@yield directive. You may pass the default value as the second argument:
@yield(‘section‘, ‘Default Content‘);
更多:
http://v4.golaravel.com/docs/4.1/templates#controller-layouts