This was a plugin I have used with CI and Twig.
Installed as plugins/twig_pi.php however with CI 2.0 you may need to load it as a package from the third_party directory or even modify it to become a helper. It is not designed to be instantiated as a CI library.
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
include_once 'Twig/Autoloader.php';
Twig_Autoloader::register();
class mxView
{
private $config, $twig;
function __construct()
{
$this->config =& get_instance()->config;
$reload = TRUE;
$cache = $this->config->item('cache_path');
$templates = FCPATH.$this->config->item('theme','settings').'views/';
$environment = array(
'debug' => FALSE,
'trim_blocks' => FALSE,
'charset' => $this->config->item('charset'),
'base_template_class' => 'Twig_Template',
);
$loader = new Twig_Loader_Filesystem($templates, $cache, $reload);
$this->twig = new Twig_Environment($loader, $environment);
}
function load($template, $data = array(), $return = FALSE)
{
$view = $this->twig->loadTemplate($template);
$data = array_merge($this->config->item('settings'), $data);
return ($return) ? $view->render($data) : $view->display($data);
}
}
Usage:
$view = new mxView();
$data['widget'] = new mxWidget();
$this->output->final_output = $view->load('layout.tpl', $data, TRUE);