2014-02-19 92 views
0

我有一個「基地」模板,其包括一個頁腳和頭:佈局文件夾Laravel 4刀片

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

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

@yield('body') 

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

我設法模板放置base.blade.php在「佈局工作'文件夾。我的具體文件遵循這種結構:

@extends('layouts.base') 

@section('body') 

@stop 

This Works。但是,如果將header.blade.php和footer.blade.php放置在佈局文件夾上(當前位於/ views文件夾的根目錄中),該模板不起作用。由於它們也是佈局的一部分,我想將它們包含在那裏。

1.)我該怎麼做?

2.)我怎樣才能將我的特定視圖分開,例如通過控制器或任意邏輯來分割它們?

實施例:

views/ 
    layout/ 
    controller_1_views/ 
    controller_2_views/ 
    controller_3_views/ 

回答

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.*路徑與相對*路徑。

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

使用該目錄結構,內部指標.blade.php,我們可以使用@rinclude('foo')或@rinclude('dir1.foo1')

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