Laravel 4 - 使用刀片模板扩展布局

时间:2014-07-28 06:47:49

标签: laravel laravel-4 blade

我刚刚设置了我的Laravel 4.2应用程序,并且正在关注一些有关身份验证的在线教程。

教程告诉我添加

protected $layout = "layouts.main";

然后在调用视图时,将其称为

$this->layout->content = View::make('users.register');

但是,如果我在创建模板时遵循Laravel网站,它会告诉我添加

@extends('layouts.main')

在我的用户/注册视图的开头

如果我正在使用@extends调用,我是否需要打扰我在开头添加的2位代码?

我真的很困惑。

干杯

1 个答案:

答案 0 :(得分:3)

不,医生不会说你应该这样做 您可以定义控制器布局(a),也可以返回扩展布局(b)

的视图

A:控制器布局

/**
 * 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');
}

B:查看扩展布局视图

// app/views/layout/master.blade.php
<html>
    <head>
        <title></title>
    </head>
    <body>
        <header>
            @yield('header')
        </header>
        <section>
            @yield('content')
        </section>
        <footer>
            @yield('footer')
        </footer>
    </body>
</html>

// app/views/profile.blade.php
@extends('layout/master')

@section('header')
    Header content here
@stop

@section('content')
    Master content here
    @if (Auth::user()->isAdmin)
        @include('admin-panel')
    @endif
@stop

@section('footer')
    Footer content here
@stop

// Controller
/**
 * Show the user profile.
 */
public function showProfile()
{
    return View::make('profile');
}