3

私のページコントローラーには

$this->layout->content = View::make('authentication/login')->with('page_title','title');

テンプレートにブレード ファイルを使用しています。htmlの頭に、私は持っています

<title>{{$page_title}}</title>

$page_title未定義 のエラーが発生します。

私が理想的に欲しいのは$data=array('page_title'=>'Login','second_item'=>'value').... しかし、ビューへの変数の基本的な受け渡しが機能しないため、まずそれを固執しています。

4

4 に答える 4

3

@Gravyが指摘したように、これを達成する方法はたくさんありますが、彼女がコードを書こうとしている方法から判断すると、解決策は次のようになります。

$data = array();
$this->layout->with('data', $data);
$this->layout->content = View::make('home');

詳細はこちら: http://forums.laravel.io/viewtopic.php?pid=58548#p58548

于 2013-10-17T14:43:51.753 に答える
3
$data = 
[
    'page_title' => 'Login',
    'second_item' => 'value'
    ...
];

return View::make('authentication/login', $data);

// or

return View::make('authentication/login', compact('data'));

// or

return View::make('authentication/login')->with($data);

// or

return View::make('authentication/login')->with(['page_title' => 'Login', 'second_item' => 'value']);

// or

return View::make('authentication/login')->with(array('page_title' => 'Login', 'second_item' => 'value'));
于 2013-10-17T13:42:55.160 に答える
1
$data = array('page_title'=>'Login','second_item'=>'value');
return View::make('authentication/login', $data);
于 2013-10-17T14:00:08.503 に答える
0

そのため、コントローラーでレイアウトを機能させるには、最初contentにレイアウト ブレード テンプレートで変数を宣言する必要があります。

コントローラーで既に行ったことを行いますが、ビューでディレクトリ構造を操作するときはドット表記を覚えておいてください。layouts.master は、layouts/master.blade.php と同じです。

class UserController extends BaseController {
    /**
     * The layout that should be used for responses.
     */
    protected $layout = 'layouts.master';

    public function getIndex()
    {
        // Remember dot notation when building views
        $this->layout->content = View::make('authentication.login')
                                     ->with('page_title','title');
    }
}

layouts/master.blade.php

<div class="content">
    {{-- This is the content variable used for the layout --}}
    {{ $content }}
</div>

authentication/login.blade.php

<title>{{ $page_title }}</title>

この構造を使用すると、これが機能します。

于 2013-10-17T14:48:38.393 に答える