Skip to content

Instantly share code, notes, and snippets.

@dshoreman
Created September 21, 2012 19:55
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save dshoreman/3763540 to your computer and use it in GitHub Desktop.
Save dshoreman/3763540 to your computer and use it in GitHub Desktop.
Custom loader class for CodeIgniter that implements Laravel's Blade templating engine
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['views_path'] = APPPATH . 'views/blade/';
$config['cache_path'] = APPPATH . 'cache/blade/';
<?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
));
}
}
<?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();
}
}
<?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
);
}
}
<?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 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);
}
{
"require": {
"illuminate/blade": "dev-master",
"illuminate/filesystem": "dev-master"
}
}
<?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
));
}
}
@jkrehm
Copy link

jkrehm commented Nov 8, 2013

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!

@marcuxyz
Copy link

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:

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