Skip to content

Instantly share code, notes, and snippets.

@tdchien
Forked from dshoreman/app-config-blade.php
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdchien/af6572b7d888804a15cf to your computer and use it in GitHub Desktop.
Save tdchien/af6572b7d888804a15cf 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
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment