Pregunta

I have a 'base' template that includes a footer and a header:

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

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

@yield('body')

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

I managed to get the template to work by placing base.blade.php on a 'layouts' folder. My specific files follow this structure:

@extends('layouts.base')

@section('body') 

@stop

This works. However, the template does not work if I place the header.blade.php and footer.blade.php on the layouts folders (Currently in the root of the /views folder). Since they are also part of the layout, I would like to include them there.

1.) How can I do that?

2.) How can I separate my specific views to divide them a little bit, say, by controller or by any arbitrary logic?

Example:

views/
    layout/
    controller_1_views/
    controller_2_views/
    controller_3_views/
¿Fue útil?

Solución

The @include syntax goes from Laravel's root view directory. So with the following file structure:

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

Your base.blade.php layout file must look like this:

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

Note the absolute layout.* paths vs. the relative * path.

Otros consejos

Here is my @rinclude statement, use it to include view files under the directory of current compiled view file or sub directories of it , work fine with 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);
});

With this directory structure, inside index.blade.php, we can use @rinclude('foo') or @rinclude('dir1.foo1')

-/app/views/my-base-view
                   └── index.blade.php
                   └── foo.blade.php
                   └── /dir1
                           └── foo1.blade.php
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top