First pass at Blade templating
This commit is contained in:
@@ -4,6 +4,9 @@ namespace App;
|
||||
|
||||
use Roots\Sage\Template;
|
||||
use Roots\Sage\Template\Wrapper;
|
||||
use Illuminate\Container\Container;
|
||||
use Illuminate\Filesystem\Filesystem;
|
||||
use Roots\Sage\Template\ViewServiceProvider;
|
||||
|
||||
/**
|
||||
* Add <body> classes
|
||||
@@ -32,11 +35,62 @@ add_filter('excerpt_more', function () {
|
||||
});
|
||||
|
||||
/**
|
||||
* Use theme wrapper
|
||||
* Use Blade template engine
|
||||
*/
|
||||
add_filter('template_include', function ($main) {
|
||||
if (!is_string($main) && !(is_object($main) && method_exists($main, '__toString'))) {
|
||||
return $main;
|
||||
foreach(['index', '404', 'archive', 'author', 'category', 'tag', 'taxonomy', 'date', 'home', 'front_page', 'page', 'paged', 'search', 'single', 'singular', 'attachment'] as $type) {
|
||||
add_filter("{$type}_template_hierarchy", function($templates) {
|
||||
foreach ($templates as $template) {
|
||||
$templates[] = str_replace('.php', '.blade.php', $template);
|
||||
}
|
||||
return $templates;
|
||||
});
|
||||
}
|
||||
add_filter('template_include', function ($template) {
|
||||
$blade_template = (!strpos($template, '.blade.php')) ? str_replace('.php', '.blade.php', $template) : $template;
|
||||
$blade_template = locate_template(basename($blade_template));
|
||||
|
||||
if (!file_exists($blade_template)) {
|
||||
return $template;
|
||||
}
|
||||
return ((new Template(new Wrapper($main)))->layout());
|
||||
}, 109);
|
||||
|
||||
$container = Container::getInstance();
|
||||
|
||||
$container->singleton('files', function () {
|
||||
return new Filesystem;
|
||||
});
|
||||
|
||||
$provider = new ViewServiceProvider($container);
|
||||
$provider->register();
|
||||
|
||||
$template_engine = $container->make('view');
|
||||
|
||||
$template_name = basename(str_replace('.blade', '', $blade_template));
|
||||
$template_name = str_replace('.php', '', $template_name);
|
||||
$html = $template_engine->make($template_name, apply_filters('laravel/blade/template_data', []))->render();
|
||||
|
||||
if (!$html) {
|
||||
return $template;
|
||||
}
|
||||
|
||||
echo $html;
|
||||
|
||||
return false;
|
||||
}, 1000);
|
||||
|
||||
add_filter('comments_template', function ($theme_template) {
|
||||
$container = Container::getInstance();
|
||||
$template_engine = $container->make('view');
|
||||
|
||||
$template_name = basename(str_replace('.blade.php', '', $theme_template));
|
||||
$html = $template_engine->make('partials/'.$template_name, []);
|
||||
var_dump($template_engine->exists('partials/'.$template_name));
|
||||
$engine = $html->getEngine();
|
||||
$compiler = $engine->getCompiler();
|
||||
$template = $compiler->getCompiledPath($compiler->getPath());
|
||||
|
||||
if ($compiler->isExpired($template)) {
|
||||
$compiler->compile($theme_template);
|
||||
}
|
||||
|
||||
return $template;
|
||||
});
|
||||
|
||||
132
src/lib/Sage/Template/ViewServiceProvider.php
Normal file
132
src/lib/Sage/Template/ViewServiceProvider.php
Normal file
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
namespace Roots\Sage\Template;
|
||||
|
||||
use Illuminate\View\Factory;
|
||||
use Illuminate\Events\Dispatcher;
|
||||
use Illuminate\View\FileViewFinder;
|
||||
use Illuminate\View\Engines\PhpEngine;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Illuminate\View\Engines\CompilerEngine;
|
||||
use Illuminate\View\Engines\EngineResolver;
|
||||
use Illuminate\View\Compilers\BladeCompiler;
|
||||
|
||||
class ViewServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Register the service provider.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$this->registerEngineResolver();
|
||||
|
||||
$this->registerViewFinder();
|
||||
|
||||
$this->registerFactory();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the engine resolver instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function registerEngineResolver()
|
||||
{
|
||||
$this->app->singleton('view.engine.resolver', function () {
|
||||
$resolver = new EngineResolver;
|
||||
|
||||
// Next we will register the various engines with the resolver so that the
|
||||
// environment can resolve the engines it needs for various views based
|
||||
// on the extension of view files. We call a method for each engines.
|
||||
foreach (['php', 'blade'] as $engine) {
|
||||
$this->{'register'.ucfirst($engine).'Engine'}($resolver);
|
||||
}
|
||||
|
||||
return $resolver;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the PHP engine implementation.
|
||||
*
|
||||
* @param \Illuminate\View\Engines\EngineResolver $resolver
|
||||
* @return void
|
||||
*/
|
||||
public function registerPhpEngine($resolver)
|
||||
{
|
||||
$resolver->register('php', function () {
|
||||
return new PhpEngine;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the Blade engine implementation.
|
||||
*
|
||||
* @param \Illuminate\View\Engines\EngineResolver $resolver
|
||||
* @return void
|
||||
*/
|
||||
public function registerBladeEngine($resolver)
|
||||
{
|
||||
$app = $this->app;
|
||||
|
||||
// The Compiler engine requires an instance of the CompilerInterface, which in
|
||||
// this case will be the Blade compiler, so we'll first create the compiler
|
||||
// instance to pass into the engine so it can compile the views properly.
|
||||
$app->singleton('blade.compiler', function ($app) {
|
||||
$cache = apply_filters('roots/views/blade/compiled_path', wp_upload_dir()['basedir'] . '/cache/compiled');
|
||||
if (!file_exists($cache)) {
|
||||
wp_mkdir_p($cache);
|
||||
}
|
||||
|
||||
return new BladeCompiler($app->make('Illuminate\\Filesystem\\Filesystem'), $cache);
|
||||
});
|
||||
|
||||
$resolver->register('blade', function () use ($app) {
|
||||
return new CompilerEngine($app['blade.compiler']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the view finder implementation.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function registerViewFinder()
|
||||
{
|
||||
$this->app->bind('view.finder', function ($app) {
|
||||
$paths = apply_filters('roots/views/blade/template_directories', [TEMPLATEPATH]);
|
||||
|
||||
return new FileViewFinder($app['files'], $paths);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the view environment.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function registerFactory()
|
||||
{
|
||||
$this->app->singleton('view', function ($app) {
|
||||
// Next we need to grab the engine resolver instance that will be used by the
|
||||
// environment. The resolver will be used by an environment to get each of
|
||||
// the various engine implementations such as plain PHP or Blade engine.
|
||||
$resolver = $app['view.engine.resolver'];
|
||||
|
||||
$finder = $app['view.finder'];
|
||||
|
||||
$env = new Factory($resolver, $finder, new Dispatcher($app));
|
||||
|
||||
// We will also set the container instance on this view environment since the
|
||||
// view composers may be classes registered in the container, which allows
|
||||
// for great testable, flexible composers for the application developer.
|
||||
$env->setContainer($app);
|
||||
|
||||
$env->share('app', $app);
|
||||
|
||||
return $env;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user