Use hyphen in view paths as delimiter for fallback views

This commit is contained in:
QWp6t
2016-12-08 15:22:17 -08:00
parent 99752388bc
commit 7f7ce45ed4
4 changed files with 60 additions and 2 deletions

View File

@@ -4,6 +4,7 @@ namespace Roots\Sage\Template;
use Jenssegers\Blade\Blade;
use Illuminate\View\Engines\CompilerEngine;
use Illuminate\Contracts\Container\Container as ContainerContract;
class BladeProvider extends Blade
{
@@ -13,6 +14,19 @@ class BladeProvider extends Blade
/** @var string */
protected $cachePath;
/**
* Constructor.
*
* @param array $viewPaths
* @param string $cachePath
* @param ContainerContract $container
*/
public function __construct($viewPaths, $cachePath, ContainerContract $container = null)
{
parent::__construct($viewPaths, $cachePath, $container);
$this->registerViewFinder();
}
/**
* @param string $view
* @param array $data
@@ -59,6 +73,20 @@ class BladeProvider extends Blade
return $compiledPath;
}
/**
* Register the view finder implementation.
*
* @return void
*/
public function registerViewFinder()
{
$this->container->bind('view.finder', function ($app) {
$paths = $app['config']['view.paths'];
return new FileViewFinder($app['files'], $paths);
});
}
/**
* @param string $file
* @return string

View File

@@ -0,0 +1,30 @@
<?php
namespace Roots\Sage\Template;
class FileViewFinder extends \Illuminate\View\FileViewFinder
{
public $possible_parts_delimiter = '-';
/**
* Get an array of possible view files.
*
* @param string $name
* @return array
*/
protected function getPossibleViewFiles($name)
{
$parts = explode($this->possible_parts_delimiter, $name);
$templates[] = array_shift($parts);
foreach ($parts as $i => $part) {
$templates[] = $templates[$i].$this->possible_parts_delimiter.$part;
}
rsort($templates);
return call_user_func_array('array_merge', array_map(function ($template) {
return array_map(function ($extension) use ($template) {
return str_replace('.', '/', $template).'.'.$extension;
}, $this->extensions);
}, $templates));
}
}

View File

@@ -11,7 +11,7 @@
@endif
@while (have_posts()) @php the_post() @endphp
@include ('partials.content')
@include ('partials.content-'.(get_post_type() !== 'post' ? get_post_type() : get_post_format()))
@endwhile
{!! get_the_posts_navigation() !!}

View File

@@ -2,7 +2,7 @@
@section('content')
@while(have_posts()) @php the_post() @endphp
@include('partials/content-single')
@include('partials/content-single-'.get_post_type())
@endwhile
@endsection