-
-
Save tdchien/af6572b7d888804a15cf 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 | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment