Skip to content

Instantly share code, notes, and snippets.

@willmendesneto
Created April 11, 2013 12:14
Show Gist options
  • Save willmendesneto/5362903 to your computer and use it in GitHub Desktop.
Save willmendesneto/5362903 to your computer and use it in GitHub Desktop.
Library para trabalhar com Twig no Framework Codeigniter
<?php if (!defined('BASEPATH')) {exit('No direct script access allowed');}
class Twig {
/**
* Referência da instância da classe CodeIgniter
*
* @var object
*/
protected $CI;
/**
* Referência da instância da classe TWIG
*
* @var object
*/
protected $_twig;
/**
* Diretório de templates da aplicação
*
* @var string
*/
protected $_templateDirectory;
/**
* Diretório do cache dos templates da aplicação
*
* @var string
*/
protected $_cacheDirectory;
/***
* Construtor da classe
*
* @param bool $debug verifica o valor do atributo DEBUG para a classe de template TWIG
* @return
*/
public function __construct($debug = false)
{
$this->CI =& get_instance();
$this->CI->config->load('twig');
log_message('debug', "Twig Autoloader Loaded");
\Twig_Autoloader::register();
$this->_templateDirectory = $this->CI->config->item('template_dir');
$this->_cacheDirectory = $this->CI->config->item('cache_dir');
$loader = new \Twig_Loader_Filesystem($this->_templateDirectory);
$this->_twig = new \Twig_Environment($loader, array(
'cache' => $this->_cacheDirectory,
'debug' => $debug
));
$this->loadAllFunctions();
}
/**
* Renderiza o template
*
* @param string $template nome do template
* @param array $data valores a serem passados ao template
* @return void
*/
public function render($template, $data = array()) {
$template = $this->_twig->loadTemplate($template);
return $template->render($data);
}
/**
* Renderiza o template verificando o tempo gasto de execução para renderização
*
* @param string $template nome do template
* @param array $data valores a serem passados ao template
* @return void
*/
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);
}
/**
* Adiciona as funções no Twig
* @param string $name nome da função
* @return void
*/
public function addFunction($name)
{
$this->_twig->addFunction($name, new Twig_Function_Function($name));
}
/**
* Carrega todas as funções da aplicação para que o Twig as reconheça
*
* @return void
*/
public function loadAllFunctions(){
foreach(get_defined_functions() as $functions) {
foreach($functions as $function) {
$this->_twig->addFunction($function, new Twig_Function_Function($function));
}
}
return $this;
}
}
/* End of file Twig.php */
/* Location: ./application/libraries/Twig.php */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment