如何将数据注入布局?

时间:2013-05-01 00:39:58

标签: laravel laravel-4

我正在尝试将数据注入布局(而不是子视图),但我还没有找到任何看似实用的方法。

这是我目前所知道的两种完成此任务的方法。为简单起见,我将注入的数据是页面标题。

方法1

// app/controllers/HomeController.php

protected $layout = 'main';

public function index()
{
    $this->layout->title = 'Page Title';
    $this->layout->content = View::make('home');
}


// app/views/layout.blade.php
...
<title>{{ $title }}</title>
...

方法2

// app/views/home.blade.php
...
@section('title')
    Page Title
@stop
...


// app/views/layout.blade.php

...
<title>@yield('title')</title>
...

再一次,这些方法似乎都不理想,但到目前为止我还没有看到更好的方法。我觉得Laravel 必须内置一些方法来处理这个......

3 个答案:

答案 0 :(得分:1)

您可以编辑BaseController中的setupLayout方法,将数据直接传递给View :: make调用。

protected function setupLayout()
{
    if(!is_null($this->layout))
    {
        $data = array(
            'title' => 'Page Title'
        );

        $this->layout = View::make($this->layout, $data);
    }
}

或者,您可以更改setupLayout方法以访问$ this-&gt; layoutData:

protected function setupLayout()
{
    if(!is_null($this->layout))
    {
        $this->layout = View::make($this->layout, $this->layoutData);
    }
}

然后在实际控制器中设置以下内容:

$this->layoutData = array('title' => 'Page Title');

答案 1 :(得分:0)

你的意思是这样吗?

<!--app/views/layouts/master.blade.php-->

<html>
    <head><title>{{ $title }}</title></head>
    <body>
        <ul id="nav">
            @section('sidebar')
                <li>List Item One</li>
            @show
       </ul>

        <div class="container">
            @yield('content')
        </div>
    </body>
</html>

<强>控制器:

public function showWelcome()
{
    $data['title'] = 'LaravelFour';
    $data['newItem'] = 'New List Item';
    return View::make('home', $data);
}

这是home.blade.php,注意@parent将输出<li>List Item One</li>{{ $newItem }}中的变量home.blade.php将替换为{{1}的值}},表示$data['newItem']

New List Item

布局中的<!--app/views/home.blade.php--> @extends('layouts.master') @section('sidebar') @parent <!--This will represent <li>List Item One</li> --> <li>{{ $newItem }}</li> <!--This is new data, will be injected in the layout--> @stop @section('content') <p>This is my body content.</p> @stop 将是

sidebar

我认为,这样您就可以在主布局中注入新数据is this what you are talking about。您也可以查看View Composersthis answer on SO

答案 2 :(得分:0)

您还可以使用View的嵌套方法...

命名空间: 照亮\查看

位于 Illuminate / View / View.php

nest( string $key, string $view, array $data = array() )
//Add a view instance to the view data.

<强>布局/ bluePrint.blade.php

<html>
<div>
 <!--Some nested content goes here -->
{{$nested_content}}
</div>
</html>

<强>形式/ loginForm.blade.php

<h2>Login</h2>
{{Form::open(array
    ('route' => $form_route, 'method' => 'POST', 'role'=>'form'))}}

<div class="form-group">
    {{Form::label('username', 'Username', array
        ('class' => 'form-control'))}}
    {{Form::text('username', $value = NULL , array
        ('class' => 'form-control', 'placeholder'=>'Username'))}}
</div>
<div class="form-group">
    {{Form::label('password', 'Password', array
        ('class' => 'form-control'))}}
    {{Form::password('password' , array
        ('class' => 'form-control', 'placeholder'=>'Password'))}}
</div>
<div class="form-group">
    {{Form::submit('GO', array('class'=>'btn btn-default'))}}
</div>
{{Form::close()}}

<强> routes.php文件

Route::get('test', array('as' => 'test', function() {
    $layout = View::make('layouts.blueprint');
    return $layout->nest('nested_content','forms.loginForm');
}));
相关问题