Templating in Laravel

前端 未结 2 2233
生来不讨喜
生来不讨喜 2020-12-02 15:54

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

相关标签:
2条回答
  • 2020-12-02 16:32

    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
    
    0 讨论(0)
  • 2020-12-02 16:37

    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/

    0 讨论(0)
提交回复
热议问题