Laravel 4 Blade的布局文件夹

时间:2014-02-19 23:42:51

标签: laravel blade

我有一个'基础'模板,其中包含页脚和标题:

// Current file is called 'base.blade.php'

// header.blade.php
@include('header')

@yield('body')

// header.blade.php
@include('footer')

我设法通过将base.blade.php放在'layouts'文件夹上来使模板工作。我的具体文件遵循以下结构:

@extends('layouts.base')

@section('body') 

@stop

这很有效。但是,如果我将header.blade.php和footer.blade.php放在布局文件夹(当前位于/ views文件夹的根目录中),模板将不起作用。由于它们也是布局的一部分,我想将它们包含在那里。

1。)我该怎么做?

2.。)我如何将我的具体观点分开来将它们分成一些,比如控制器或任意逻辑?

示例:

views/
    layout/
    controller_1_views/
    controller_2_views/
    controller_3_views/

2 个答案:

答案 0 :(得分:2)

@include语法来自Laravel的根视图目录。所以使用以下文件结构:

views/
  --layout/
  ----base.blade.php
  ----header.blade.php
  ----footer.blade.php
  --controller_1_views/
  --controller_2_views/
  --controller_3_views/

您的base.blade.php布局文件必须如下所示:

@include('layout.header')
@yield('body')
@include('layout.footer')

请注意绝对layout.*路径与相对*路径。

答案 1 :(得分:0)

这是我的@rinclude语句,用它来包含当前编译视图文件目录下的视图文件或它的子目录,与Laravel 4.2一起正常工作

Blade::extend(function($view, $compiler)
{
    $viewBasePath = Config::get('view.paths')[0];
    $curCompiledFilePath = $compiler->getPath();
    $paths = explode('/', substr($curCompiledFilePath, strlen($viewBasePath)), -1);
    $basePath = '';
    foreach($paths as $path) {
        $basePath .= $path . '.';
    }
    $basePath = trim($basePath, '.');
    $basePath = "'$basePath.'.";
    $pattern = $compiler->createMatcher('rinclude');
    return preg_replace($pattern, "<?php echo \$__env->make($basePath$2, array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>", $view);
});

使用这个目录结构,在index.blade.php里面,我们可以使用@rinclude('foo')或@rinclude('dir1.foo1')

-/app/views/my-base-view
                   └── index.blade.php
                   └── foo.blade.php
                   └── /dir1
                           └── foo1.blade.php