【问题标题】:Proper Reusable Subview with Laravel's Blade Template System使用 Laravel 的 Blade 模板系统的可重用子视图
【发布时间】:2014-11-23 16:24:26
【问题描述】:

我有一个登录表单,我希望在各种情况下重复使用它。例如,我想要一个只有登录的页面 (www.mysite.com/login),但也想使用我的首页 (www.mysite.com) 上的登录表单。

Laravel 的文档提出了以下方法来为用户配置文件使用子视图。

<!-- Stored in app/controllers/UserController.php -->

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

}

<!-- 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>

<!-- Stored in app/views/user/profile.blade.php -->

@extends('layouts.master')

@section('sidebar')
    <p>This is appended to the master sidebar.</p>
@stop

@section('content')
    <p>This is my body content.</p>
@stop

问题是子视图被代码“污染”,使其成为 layouts.master 视图的子视图。由于我希望我的登录表单可以在各种布局中重复使用,因此我不希望对应该使用它的特定布局进行任何引用。

我想我可以在视图中将它称为视图变量:

{{ $login }}

然后用这样的控制器实现它:

$this->someview->login = View::make('user.login');

因此我的登录视图可能是纯粹的:

 <!-- Stored in app/views/user/login.php -->

<form method="post" action="{{ URL::to('/') }}/login" accept-charset="utf-8">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    <fieldset>
        <legend>credentials</legend>
        <label>
            <span>email</span>
            <input name="email" type="email" placeholder="yourname@email.com" tabindex="1" required autofocus>
        </label>
        <label>
            <span>password</span>
            <input name="password" type="password" placeholder="password" tabindex="2" required>
        </label>
    </fieldset>
    <input type="submit" value="login">
</form>

这是插入子视图而不对应该使用的布局进行硬编码的最佳方法吗?

老实说,我可以看到为特定视图定义布局的强大功能,因为它提供了加载视图和系统自动查找布局的可能性,但这不违背干编码的概念吗?

【问题讨论】:

    标签: php laravel blade


    【解决方案1】:

    您可以使用刀片@include 语法将任何视图包含到另一个视图中。 Laravel docs(你必须向下滚动一点)

    @include('user.login')
    

    如果你愿意,也可以传入参数...

    @include('user.login', array('foo' => 'bar'))
    

    ...然后可以在包含的视图中作为变量访问

    {{ $foo }}
    

    更新

    例如,对于您的登录页面,您将创建一个login.blade.php。 (我会将包含表单的登录子视图放在子目录中。例如partials

    @extends('layouts.master')
    
    @section('content')
        @include('partials.login')
    @stop
    

    在您的控制器操作中,您只需返回登录(页面)视图

    return View::make('login'); // NOT partials.login
    

    而对于需要登录表单的其他页面,只需执行正确的操作即可。

    【讨论】:

    • 是的,但这弄乱了我的布局。该布局也用于许多页面。我希望我的布局是不可知的,我的子视图也是。我正在考虑在布局中有一个 {{ $content }} 并使用控制器来加载我的视图。不知道我是否清楚。
    • 不要放在布局中。请查看更新后的答案
    • 嗯嗯……我明白了。有趣的。所以这将给我最好的两个词,能够调用登录视图,它会自动加载我的布局,但仍然有一个干净的部分。谢谢一堆。我不明白这个片面的想法。我标记了你的答案。
    猜你喜欢
    • 1970-01-01
    • 2014-06-20
    • 2015-06-25
    • 2013-06-18
    • 2017-12-06
    • 2013-06-01
    • 1970-01-01
    相关资源
    最近更新 更多