Adding template engine Twig on CodeIgniter


1. Download Twig vendor from http://twig.sensiolabs.org/

2. Extract the files and copy the contents of the Twig-1.x.xlib to /application/libraries/

Your library should have the Twig folder inside /libraries/Twig





3. Create a Twig.php inside the libraries folder then add the following code:

class Twig
{
private $CI;
private $_twig;
private $_template_dir;
private $_cache_dir;

public function __construct($debug = false)
{
$this->CI =& get_instance();

self::requireTwigAutoloader();

Twig_Autoloader::register();

$this->CI->config->load(twig);

$this->_template_dir = $this->CI->config->item(template_dir);
$this->_cache_dir = $this->CI->config->item(cache_dir);

$loader = new Twig_Loader_Filesystem($this->_template_dir);

$this->_twig = new Twig_Environment($loader, array(
cache => $this->_cache_dir,
debug => true,
));

$this->_twig->addExtension(new Twig_Extension_Debug());

// enable all php function on twig
foreach(get_defined_functions() as $functions) {
foreach($functions as $func) {
$this->_twig->addFunction($func, new Twig_Function_Function($func));
}
}

// add session on twig template
$this->_twig->addGlobal("session", $this->CI->session);
}

public function add_function($name)
{
$this->_twig->addFunction($name, new Twig_Function_Function($name));
}

public function render($template, $data = array())
{
$template = $this->_twig->loadTemplate($template);
return $template->render($data);
}

public function display($template, $data = array())
{
$template = $this->_twig->loadTemplate($template);

/* elapsed_time and memory_usage */
$data[elapsed_time] = $this->CI->benchmark->elapsed_time(
total_execution_time_start,
total_execution_time_end
);

$data[memory_usage] = 0;
if (function_exists(memory_get_usage)) {
$memory = memory_get_usage() / 1024 / 1024;
$data[memory_usage] = round($memory, 2) . MB;
}

$template->display($data);
}

private static function requireTwigAutoloader()
{
$twig_dir = PATH_SEPARATOR . APPPATH . libraries/Twig;
ini_set(include_path, ini_get(include_path) . $twig_dir);

require_once (string) "Autoloader" . EXT;

log_message(debug, "Twig Autoloader Loaded");
}
}

4. Create another file in application/config folder named twig.php and copy the following code:

$config[template_dir] = APPPATH.views;
$config[cache_dir] = APPPATH.cache/twig;

5. Open the application/config/autoload.php and add twig on your array of libraries

$autoload[libraries] = array(database,session,twig);

6. Now call the twig on your controller like this:

$this->twig->display(
products/index.html.twig,
array(products => $products, category => $category)
);
or
$this->twig->display(
products/index.html.twig,
get_defined_vars(),
);

The first paramater is the path to your view file and the second parameter is an array of variables you want to pass on that view file.

7. You can then manipulate the data in your application/views/products/index.html.twig folder:

{% for product in products %}
{{ product.name }}
{% endfor %}
download
alternative link download