Skip to content

Twig PHP Template Engine Implementation

Dimas R. Danu edited this page May 23, 2015 · 1 revision

Category:Libraries::Template Engine

Introduction

Twig is PHP Template Engine.
Template Engine provides a cleaner syntax and additional features for your view files
This is the smallest and quickest way to implement it in Codeigniter 3.0.0
Note that this only tested on Codeigniter 3.0.0
All file below available at Codeigniter-Twig

Installation

  1. Add Twig to composer requires and "application/vendor" to vendor-dir config.

    "config": {
    	"vendor-dir": "application/vendor"
    },
    "require": {
    	"twig/twig": "~1.0"
    }
  2. Run Composer.

  3. Enable composer_autoload in config file.

    $config['composer_autoload'] = TRUE;
  4. Create a new config file in application/config and name it twig.php. And add the following code:

    $config['twig']['template_dir'] = VIEWPATH;
    $config['twig']['template_ext'] = 'php';
    $config['twig']['environment']['autoescape'] = TRUE;
    $config['twig']['environment']['cache'] = FALSE;
    $config['twig']['environment']['debug'] = FALSE;
  5. Create a new library and name it Twig.php. And add the following code:

    class Twig {
    	private $CI;
    
    	private $_config = array();
    	private $_twig;
    	private $_twig_loader;
    
    	public function __construct(){
    		log_message('debug', 'Twig: library initialized');
    
    		$this->CI =& get_instance();
    
    		$this->_config = $this->CI->config->item('twig');
    
    		try {
    			$this->_twig_loader = new Twig_Loader_Filesystem($this->_config['template_dir']);
    		} catch (Exception $e) {
    			show_error(htmlspecialchars_decode($e->getMessage()), 500, 'Twig Exception');
    		}
    
    		if($this->_config['environment']['cache'] === true){
    			$this->_config['environment']['cache'] = APPPATH.'cache/twig';
    		}
    
    		$this->_twig = new Twig_Environment($this->_twig_loader, $this->_config['environment']);
    	}
    
    	public function render($template, $data = array()){
    		$template = $this->addExtension($template);
    		return $this->_twig->render($template, $data);
    	}
    
    	public function display($template, $data = array()){
    		$this->_twig->display($template, $data);
    	}
    
    	private function addExtension($template){
    		$ext = '.'.$this->_config['template_ext'];
    
    		if(substr($template, -strlen($ext)) !== $ext){
    			return $template .= $ext;
    		}
    
    		return $template;
    	}
    }
  6. Create core file to extend Codeigniter Loader. In this example is MY_Loader.php.

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class MY_Loader extends CI_Loader {
    	public function view($template, $data = array(), $return = FALSE) {
    		$CI =& get_instance();
    
    		try {
    			$output = $CI->twig->render($template, $data);
    		} catch (Exception $e) {
    			show_error(htmlspecialchars_decode($e->getMessage()), 500, 'Twig Exception');
    		}
    
    		// Return the output if the return value is TRUE.
    		if ($return === TRUE) {
    			return $output;
    		}
    
    		// Otherwise append to output just like a view.
    		$CI->output->append_output($output);
    	}
    }
  7. Add twig.php config file and Twig.php library to your autoload configuration.

  8. Now try to use Twig's syntax in your view files.

This does not change a code of your controller since you still using load->view('view_file', $data) to load a view. This means you can add Twig even when your application is running and still can use caching, compression, etc.
I made a small repository of every file I use to implement Twig complete with documentation of the config file.
For information about Twig and how to use it, please visit Twig's Documentation

Credit

  1. Codeigniter 3.0.0
  2. Twig PHP Template Engine
  3. Codeigniter_Smarty
  4. Codeigniter-Twiggy
  5. If you think your name should be here, feel free to contact me
Clone this wiki locally