Using Twig library with Codeigniter

Quite few days back worked on TWIG the template engine for PHP and tried if can be used in my existing codeigniter setup

Want to share following quick steps which may be useful for using with codeigniter

Step 1

Create Twig cache directory under “application / cache ” folder and make sure its writable..

Step 2

Download the TWIG library HERE and put in libraries folder so it will follow following directory structure

application/libraries/Twig
|-- Error
|-- Extension
|-- Filter
|-- Function
|-- Loader
|-- Node
|   `-- Expression
|       |-- Binary
|       `-- Unary
|-- NodeVisitor
|-- Sandbox
|-- Test
`-- TokenParser

Now create Library files

File 1=> application/libraries/Twig.php

################################################

<?php if (!defined(‘BASEPATH’)) {exit(‘No direct script access allowed’);}
class Twig
{
private $CI;
private $_twig;
private $_template_dir;
private $_cache_dir;
/**
* Constructor
*
*/
function __construct($debug = false)
{
$this->CI =& get_instance();
$this->CI->config->load(‘twig’);
ini_set(‘include_path’,
ini_get(‘include_path’) . PATH_SEPARATOR . APPPATH . ‘libraries/Twig’);
require_once (string) “Autoloader” . EXT;
log_message(‘debug’, “Twig Autoloader Loaded”);
Twig_Autoloader::register();
$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’ => $debug,
));
foreach(get_defined_functions() as $functions) {
             foreach($functions as $function) {
                 $this->_twig->addFunction($function, new Twig_Function_Function($function));
             }
         }
}
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’);
$memory = (!function_exists(‘memory_get_usage’)) ? ‘0’ : round(memory_get_usage()/1024/1024, 2) . ‘MB’;
$data[‘memory_usage’] = $memory;
$template->display($data);
}
}
###########################################

File 2 application/config/twig.php

##############################################
<?php if (!defined(‘BASEPATH’)) exit(‘No direct script access allowed’);
$config[‘template_dir’] = APPPATH.’views’;
$config[‘cache_dir’] = APPPATH.’cache/twig’;
###################################3

Step 3 USAGE

Put in you controller
$this->load->library('twig');

$data['title'] = "twig loaded";

$this->twig->display('view.html', $data);


simple huh…..

 

reference 

Advertisements

What you think ? Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s