Skip to content

Instantly share code, notes, and snippets.

@MuhammadSaim
Created July 30, 2021 05:21
Show Gist options
  • Save MuhammadSaim/dabafdf341d39bb9571afec2a9c80a13 to your computer and use it in GitHub Desktop.
Save MuhammadSaim/dabafdf341d39bb9571afec2a9c80a13 to your computer and use it in GitHub Desktop.
Symfony Twig Templating Engine with Codeigniter 3
<?php
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
use Twig\TwigFunction;
/**
* Create My_controller.php file in the application/core folder
* please change the subclass_prefix in application/config/config.php from MY to My
* or if you want as it is please name this file and class with MY_Controller.
*
*
*/
class My_Controller extends CI_Controller {
protected $loader;
protected $twig;
protected $view_path = APPPATH . "views";
protected $cache_path = APPPATH . "cache";
protected $CI;
public function __construct() {
parent::__construct();
$this->loader = new FilesystemLoader( $this->view_path );
$this->twig = new Environment( $this->loader, [
'template_ext' => 'twig',
'debug' => true
] );
$this->registerFunctions();
$this->CI = &get_instance();
}
/**
* render the template
*
* @param string $file
* @param array $data
* @return void
*/
public function render( $file, $data = [] ) {
if ( empty( $data ) ) {
echo $this->twig->render( $file );
} else {
echo $this->twig->render( $file, $data );
}
}
/**
* get all the loaded files
*
* @return array
*/
public function getLoadedHelperFiles() {
$ci_helpers = array_filter( array_map( function ( $file ) {
return stripos( $file, "_helper.php" ) !== false ?
basename( $file, ".php" ) : false;
}, get_included_files() ) );
return $ci_helpers;
}
/**
* get the all the helper functions from the loaded files
*
* @return array
*/
public function getHelpers() {
$core_helpers_path = FCPATH . "system/helpers/";
$helpers = APPPATH . "helpers/";
$files = $this->getLoadedHelperFiles();
$helper_functions = [];
foreach ( $files as $file ) {
$system_file = $core_helpers_path . $file . ".php";
$user_file = $helpers . $file . ".php";
if ( file_exists( $system_file ) ) {
$functions = $this->functions_in_file( $system_file );
$helper_functions = array_merge( $helper_functions, $functions );
}
if ( file_exists( $user_file ) ) {
$functions = $this->functions_in_file( $user_file );
$helper_functions = array_merge( $helper_functions, $functions );
}
}
return $helper_functions;
}
/**
* gather functions from the php file
*
* @param string $file
* @param boolean $sort
* @return array
*/
public function functions_in_file( $file, $sort = FALSE ) {
$file = join( "\n", file( $file ) );
preg_match_all( '/function\s+(\w+)/', $file, $m );
$functions = $m[1];
if ( $sort ) {
asort( $functions );
}
return $functions;
}
/**
* Register helpers functions to the twig
*
* @return void
*/
public function registerFunctions() {
foreach ( $this->getHelpers() as $helper ) {
try {
$this->twig->addFunction( new TwigFunction( $helper, $helper ) );
} catch ( \Exception$e ) {
continue;
}
}
}
/**
* @param array $array
* @return mixed
*/
public function JSONResponse(array $array)
{
return $this->output
->set_content_type('application/json')
->set_output(json_encode($array));
}
public function ajax()
{
return 'XMLHttpRequest' === $this->input->get_request_header('X-Requested-With');
}
}
<?php
/**
* In your controller extends you controller class with My_controller or MY_Controller
* in the application/views directory rename welcome_message.php to welcome_message.twig
*
*/
class Welcome extends My_Controller{
public function index(){
$this->render("welcome_message", [
"message" => "Welcome to Codeigniter 3 with Twig <3"
]);
}
}
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Welcome</title>
</head>
<body>
<h1>{{ message }}</h1>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment