Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
183 views
in Technique[技术] by (71.8m points)

php - Templating in Laravel

I'm trying to get my default template working with Laravel. I'm coming from Codeigniter and Phil Sturgeon's template system so I'm trying to do it in a similar way. Can anyone help me with what I'm missing/doing wrong? Thanks!

//default.blade.php (located in layouts/default)
<html>
    <title>{{$title}}</title>
    <body>
    {{$content}}
    </body>
</html>
//end default.blade.php

//home.blade.php (index view including header and footer partials)
@layout('layouts.default')
@include('partials.header')
//code
@include('partials.footer')
//end home

//routes.php (mapping route to home controller)
Route::controller( 'home' );
//end

//home.php (controller)
<?php
class Home_Controller extends Base_Controller {
    public $layout = 'layouts.default';
    public function action_index()
    {   
        $this->layout->title = 'title';
        $this->layout->content = View::make( 'home' );
    }
}
//end
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are mixing two different layout approaches of Laravel. This way you are rendering the layout view, include the home view and try to include inside again the layout.

My personal preference is the controller approach.

Controller Layouts

The controller and the layouts can remain the same.

Note: As a shortcut you could nest the content instead of View::make, that automaically renders it when you echo it out in the layout.

In home.blade.php remove the @layout function.

Edit (example):

controllers/home.php

<?php
class Home_Controller extends Base_Controller {
  public $layout = 'layouts.default';
  public function action_index()
  {
    $this->layout->title = 'title';
    $this->layout->nest('content', 'home', array(
      'data' => $some_data
    ));
  }
}

views/layouts/default.blade.php

<html>
  <title>{{ $title }}</title>
  <body>
    {{ $content }}
  </body>
</html>

views/home.blade.php

Partials are included in the content.

@include('partials.header')
{{ $data }}
@include('partials.footer')

Blade Layouts

If you want this approach you have a few problems there. First, you are including new content after the layout. Not sure if intentional, but the @layout function itself is basicly just an @include restricted to be at the very beginning of the view. So if your layout is a closed html, any include after that will be appended after your html layout.

Your content should use sections here with the @section function and @yield it in your layout. The header and footer could be included in the layout with @include or if you want to define it in the content view then put those in a @section too, like below. If you define it that way if a section doesn't exist nothing gets yielded.

controllers/home.php

<?php
class Home_Controller extends Base_Controller {
  public function action_index()
  {
    return View::make('home')->with('title', 'title');
  }
}

views/layouts/default.blade.php

<html>
 <title>{{$title}}</title>
 <body>
  @yield('header')
  @yield('content')
  @yield('footer')
 </body>
</html>

views/home.blade.php

@layout('layouts.default')
@section('header')
  header here or @include it
@endsection
@section('footer')
  footer
@endsection
@section('content')
  content
@endsection

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...