Created
September 21, 2012 19:55
-
-
Save dshoreman/3763540 to your computer and use it in GitHub Desktop.
Custom loader class for CodeIgniter that implements Laravel's Blade templating engine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
$config['views_path'] = APPPATH . 'views/blade/'; | |
$config['cache_path'] = APPPATH . 'cache/blade/'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
class Home extends CI_Controller { | |
public function index() | |
{ | |
// Prepare some test data for our views | |
$array = explode('-', date('d-m-Y')); | |
list($d, $m, $y) = $array; | |
// Basic view with no data | |
echo $this->load->blade('home.index'); | |
// Passing a single value | |
echo $this->load->blade('home.index')->with('day', $d); | |
// Multiple values with method chaining | |
echo $this->load->blade('home.index') | |
->with('day', $d) | |
->with('month', $m) | |
->with('year', $y); | |
// Passing an array | |
echo $this->load->blade('home.index', array( | |
'day' => $d, | |
'month' => $m, | |
'year' => $y | |
)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
class Home extends CI_Controller { | |
public function index() | |
{ | |
// Load the view into a variable | |
$view = $this->load->blade('home.index'); | |
// Attach some data | |
$view->with('time', date('H:i:s')); | |
// Render the parsed view | |
echo $view->get(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
require 'vendor/autoload.php'; | |
use Illuminate\Blade\Environment; | |
use Illuminate\Blade\Loader; | |
use Illuminate\Blade\View; | |
class MY_Loader extends CI_Loader { | |
public function __construct() | |
{ | |
parent::__construct(); | |
} | |
public function blade($view, array $parameters = array()) | |
{ | |
$CI =& get_instance(); | |
$CI->config->load('blade', true); | |
return new View( | |
new Environment(Loader::make( | |
$CI->config->item('views_path', 'blade'), | |
$CI->config->item('cache_path', 'blade') | |
)), | |
$view, $parameters | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require 'vendor/autoload.php'; | |
use Illuminate\Blade\Environment; | |
use Illuminate\Blade\Loader; | |
use Illuminate\Blade\View; | |
function blade($view, array $parameters = array()) | |
{ | |
return new View( | |
new Environment(Loader::make( | |
'path/to/blade/views', | |
'path/to/blade/cache' | |
)), | |
$view, $parameters | |
); | |
} | |
echo blade('home.index')->with('name', 'Dave'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This updated method fixes block comments with a newline | |
// char immediately after the opening {{-- tag. | |
protected function compileComments($value) | |
{ | |
$value = preg_replace('/\{\{--(.*?)--\}\}/', "", $value); | |
return preg_replace('/\{\{--(\n?)(.*?)--\}\}/s', "$1", $value); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"illuminate/blade": "dev-master", | |
"illuminate/filesystem": "dev-master" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Home extends CI_Controller { | |
public function index() | |
{ | |
// Prepare some test data for our views | |
$array = explode('-', date('d-m-Y')); | |
list($d, $m, $y) = $array; | |
// Basic view with no data | |
echo $this->load->blade('home.index'); | |
// Passing a single value | |
echo $this->load->blade('home.index')->with('day', $d); | |
// Multiple values with method chaining | |
echo $this->load->blade('home.index') | |
->with('day', $d) | |
->with('month', $m) | |
->with('year', $y); | |
// Passing an array | |
echo $this->load->blade('home.index', array( | |
'day' => $d, | |
'month' => $m, | |
'year' => $y | |
)); | |
} | |
} |
Problem 1
- The requested package illuminate/blade could not be found in any version, there may be a typo in the package name.
Problem 2
- Installation request for illuminate/filesystem dev-master -> satisfiable by illuminate/filesystem[dev-master].
- illuminate/filesystem dev-master requires illuminate/contracts 5.4.* -> satisfiable by illuminate/contracts[5.4.x-dev] but these conflict with your requirements or minimum-stability.
Potential causes:
- A typo in the package name
- The package is not available in a stable-enough version according to your minimum-stability setting
see https://getcomposer.org/doc/04-schema.md#minimum-stability for more details.
Read https://getcomposer.org/doc/articles/troubleshooting.md for further common problems.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think Blade got rolled into a more abstract package, so I had to make many modifications to the MY_Loader to get this work, but it's working well in my development environment now. I forked your gist and updated with my changes. Thanks for an excellent starting point!