Templating in Laravel

柔情痞子 提交于 2019-11-27 11:41:48
TLGreg

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
Raftalks

The answer given above explains how templating is done in Laravel, however to gain additional benefits like managing themes organised into a theme directory with ability to switch between themes and having partials and theme resources all together sounds like almost something similar to Phil Sturgeon Template Library for CI. You may want to check the Theme bundle for Laravel. Here is the link:

http://raftalks.github.io/Laravel_Theme_Bundle/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!