1

http://laravel.com/docs/4.2/templatesから:

(コントローラー)

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

}

(テンプレート)

@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.master2 回呼び出す必要があるのですか? $this->layoutに設定する必要があるという事実layouts.masterと、に渡す必要があるという事実は、冗長で不要なようですlayouts.master@extends()

4

1 に答える 1

5

showProfile()メソッドに入れるだけで十分です:

return View::make('user.profile');

それ以外の:

protected $layout = 'layouts.master';

$this->layout->content = View::make('user.profile');

編集

プロパティを使用する場合の別の方法$layoutは、もう少し複雑です。

layouts.masterテンプレートでは使用しませんがyield('content'){{ $content }}変数として配置するため、ファイルは次のようになります。

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
  test

 {{ $content }}

  test2

{{ $sidebar }}

</body>
</html>

これで、以前と同じようにプロパティを取得できます。

protected $layout = 'layouts.master';

content必要なのは、次を使用して何かとsidebar変数を設定することです。

$this->layout->content = 'this is content';
$this->layout->sidebar = 'this is sidebar';

自動でレイアウトが表示されます

もちろん、上記の2つのケースでは、テンプレートを使用できるため、次を使用できます。

$this->layout->content = View::make('content');
$this->layout->sidebar = View::make('sidebar');

@sectionこれらのファイルには、たとえば次のようなコンテンツを定義していません。

content.blade.php

this is content

sidebar.blade.php

this is sidebar

この出力は次のようになります。

test this is content test2 this is sidebar 

私にとってこの方法ははるかに複雑です。私は常に使用return View::make('user.profile');し、最初に示したようにテンプレートを定義しました(@section独自のコンテンツを配置するために他のテンプレートを拡張します)

于 2014-10-01T21:38:54.270 に答える