在Laravel中制作模板 [英] Templating in Laravel

查看:49
本文介绍了在Laravel中制作模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让我的默认模板与Laravel一起使用.我来自Codeigniter和Phil Sturgeon的模板系统,所以我试图以类似的方式来做.有人可以帮我解决我遗失/做错的事情吗?谢谢!

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

推荐答案

您正在混合使用Laravel的两种不同的布局方法. 这样,您将呈现布局视图,包括主视图,然后尝试再次将布局包含在内部.

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.

控制器和布局可以保持相同.

The controller and the layouts can remain the same.

注意:作为一种快捷方式,您可以嵌套内容而不是View :: make,当您在布局中回显内容时,它会自动呈现内容.

在home.blade.php中,删除@layout函数.

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

编辑(示例):

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

视图/布局/default.blade.php

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

views/home.blade.php

部分包含在内容中.

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

刀片布局

如果您想使用这种方法,那您会遇到一些问题.首先,您要在布局之后添加新内容.不确定是否是有意的,但是 @layout 函数本身基本上只是一个 @include ,被限制在视图的最开始.因此,如果您的布局是封闭的html,则此之后的所有包含内容都将附加在您的html布局之后.

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.

您的内容应在此处使用具有 @section 功能的部分,并在布局中使用 @yield .页眉和页脚可以使用 @include 包含在布局中,或者如果您想在内容视图中定义页眉和页脚,也可以将它们放在 @section 中,如下所示.如果以这种方式定义某个部分不存在,则不会产生任何结果.

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

视图/布局/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

这篇关于在Laravel中制作模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆