php 在 Laravel 中进行模板化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12524712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:43:09  来源:igfitidea点击:

Templating in Laravel

phplaravellaravel-3

提问by coryj

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!

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

//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

回答by 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.

您正在混合 Laravel 的两种不同布局方法。通过这种方式,您可以渲染布局视图,包括主视图并尝试再次将布局包含在内部。

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.

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

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

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

Edit (example):

编辑(示例):

controllers/home.php

控制器/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

视图/布局/default.blade.php

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

views/home.blade.php

意见/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 @layoutfunction itself is basicly just an @includerestricted 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.

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

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

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

controllers/home.php

控制器/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

视图/布局/default.blade.php

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

views/home.blade.php

意见/home.blade.php

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

回答by 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:

上面给出的答案解释了如何在 Laravel 中完成模板化,但是为了获得额外的好处,例如管理组织到主题目录中的主题,能够在主题之间切换以及将部分和主题资源放在一起听起来几乎类似于Phil Sturgeon Template Library for CI. 您可能需要查看 Laravel 的 Theme 包。链接在这里:

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

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