【问题标题】:Laravel 4 - Extending the layout using blade templatingLaravel 4 - 使用刀片模板扩展布局
【发布时间】:2014-09-19 07:38:52
【问题描述】:

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

教程告诉我要添加

protected $layout = "layouts.main";

然后在调用视图时,像这样调用它

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

但是,如果我按照 Laravel 网站创建模板,它会告诉我添加

@extends('layouts.main')

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

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

我真的很困惑。

干杯

【问题讨论】:

    标签: laravel laravel-4 blade


    【解决方案1】:

    不,文档并没有说你应该这样做。
    您要么定义控制器布局(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');
    }    
    

    【讨论】:

    • 哦,好吧......所以任何一种方式都是正确的取决于编写代码的人?还是上述两种方法中的更好?
    • 我更喜欢第二种方法。它更灵活(例如,查看已编辑的答案)
    • 完美...这就是我目前正在做的方式...感谢您的帮助!
    猜你喜欢
    • 2016-04-14
    • 2017-02-22
    • 2014-06-13
    • 2013-02-25
    • 2015-04-24
    • 2013-05-08
    • 1970-01-01
    • 2017-04-03
    • 1970-01-01
    相关资源
    最近更新 更多