Skip to content

Instantly share code, notes, and snippets.

@sudo-barun
Last active December 26, 2018 11:28
Show Gist options
  • Save sudo-barun/49a9649d029db1133a5a72da7d47e280 to your computer and use it in GitHub Desktop.
Save sudo-barun/49a9649d029db1133a5a72da7d47e280 to your computer and use it in GitHub Desktop.
Using Laravel Blade template in CodeIgniter and in other non-laravel projects

Use Laravel Blade template in CodeIgniter based projects

Usage:

  1. enable composer autoload in /application/config/config.php:

    $config['composer_autoload'] = TRUE;
  2. copy following files:

    1. /application/composer.json
    2. /application/config/blade.php
    3. /application/classes/BladeViewFactory.php
    4. /application/classes/BladeConfigurationInterface.php
    5. /application/classes/CodeIgniterBladeConfiguration.php
    6. /application/helpers/blade_helper.php
  3. use in a controller:

    echo blade('home.index', [
        'title' => 'Home',
    ])->render();

For PHP projects using frameworks other than CodeIgniter (and Laravel of course) or no frameworks at all:

  1. Create class that implements BladeConfigurationInterface, for example, MyBladeConfiguration.
  2. Pass object of MyBladeConfiguration to BladeViewFactory. See blade_helper.php.
<?php
namespace App;
interface BladeConfigurationInterface
{
/**
* @return array
*/
public function getViewPaths();
/**
* @return string
*/
public function getCachePath();
}
<?php
namespace App;
use Illuminate\Events\Dispatcher;
use Illuminate\Filesystem\Filesystem;
use Illuminate\View\Compilers\BladeCompiler;
use Illuminate\View\Engines\CompilerEngine;
use Illuminate\View\Engines\EngineResolver;
use Illuminate\View\Factory;
use Illuminate\View\FileViewFinder;
class BladeViewFactory
{
/** @var self */
protected static $instance;
/** @var BladeConfigurationInterface */
protected $bladeConfiguration;
/** @var \Illuminate\View\Factory */
protected $factory;
public function __construct(BladeConfigurationInterface $bladeConfiguration)
{
$this->bladeConfiguration = $bladeConfiguration;
$this->init();
}
private function init()
{
$fileSystem = new Filesystem();
$fileFinder = new FileViewFinder($fileSystem, $this->bladeConfiguration->getViewPaths());
$bladeCompiler = new BladeCompiler($fileSystem, $this->bladeConfiguration->getCachePath());
$resolver = new EngineResolver();
$dispatcher = new Dispatcher();
// register blade engine
$resolver->register('blade', function () use ($bladeCompiler) {
return new CompilerEngine($bladeCompiler);
});
$this->factory = new Factory(
$resolver,
$fileFinder,
$dispatcher
);
}
/**
* @param string $view
* @param array $parameters
* @return \Illuminate\View\View
*/
public function make($view, array $parameters = [])
{
return $this->factory->make($view, $parameters);
}
}
<?php
namespace App;
class CodeIgniterBladeConfiguration implements BladeConfigurationInterface
{
protected $configFile;
public function __construct($configFile = 'blade')
{
$this->configFile = $configFile;
}
/**
* @return \CI_Config
*/
public function getConfig()
{
static $CI;
if (is_null($CI)) {
$CI =& get_instance();
$CI->config->load($this->configFile, true);
}
return $CI->config;
}
public function getViewPaths()
{
return $this->getConfig()->item('view_paths', 'blade');
}
public function getCachePath()
{
return $this->getConfig()->item('cache_path', 'blade');
}
}
{
"autoload": {
"files": [
"helpers/blade_helper.php"
],
"psr-4": {
"App\\": "classes/"
}
},
"require": {
"illuminate/view": "^5.5"
}
}
<?php
$config['view_paths'] = [
APPPATH . 'views',
];
$config['cache_path'] = APPPATH . 'cache/blade';
<?php
if (! function_exists('blade'))
{
/**
* @param string $view
* @param array $parameters
* @return \Illuminate\View\View
*/
function blade($view, array $parameters = [])
{
return (new \App\BladeViewFactory(
new \App\CodeIgniterBladeConfiguration()
))->make($view, $parameters);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment