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'); } }