[eluser]Unknown[/eluser]
This can be useful to all who wants to use smarty templates in your ci applications.
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
require APPPATH .'libraries/Smarty-2.6.20/libs/Smarty.class'.EXT;
/**
* @file system/application/libraries/Mysmarty.php
*/
class CI_Smarty extends Smarty
{
function CI_Smarty()
{
$this->Smarty();
$this->ci =& get_instance();
$this->ci->config->load('smarty');
$config =& get_config();
// absolute path prevents "template not found" errors
$this->template_dir = (!empty($config['smarty']['template_dir']) ? $config['smarty']['template_dir'] : APPPATH . 'templates/');
$this->compile_dir = (!empty($config['smarty']['compile_dir']) ? $config['smarty']['compile_dir'] : BASEPATH . 'cache/templates_c/'); //use CI's cache folder
$this->cache_dir = (!empty($config['smarty']['cache_dir']) ? $config['smarty']['cache_dir'] : BASEPATH . 'cache/pages/'); //use CI's cache folder
$this->config_dir = (!empty($config['smarty']['config_dir']) ? $config['smarty']['config_dir'] : APPPATH . 'config/'); //use CI's cache folder
$this->plugins_dir[] = APPPATH. 'plugins'; //My Plugins
$this->debugging = $config['smarty']['debugging'];
//URL helper required
parent::assign('url', $config['base_url']); // so we can get the full path to CI easily
parent::assign_by_ref('this', $this->ci); //CI instance
}
function view($resource_name, $cache_id = null) {
if (strpos($resource_name, '.') === false) $resource_name .= '.tpl';
return parent::display($resource_name, $cache_id);
}
} // END class smarty_library
Smarty config
Code:
//Smarty
$config['smarty']['compile_check'] = true;
$config['smarty']['caching'] = false;
$config['smarty']['debugging'] = false;
//$config['smarty']['template_dir'], $config['smarty']['compile_dir']
Examples
Code:
<?php
class Example extends Controller {
function Example ()
{
parent::Controller();
$this->load->library('smarty');
$this->smarty->assign('owner', 'Me'); //or any smarty method you can use
require_once $this->smarty->_get_plugin_filepath('modifier', 'date_left');
$date_left = smarty_modifier_date_left('2009-04-03 13:24:00');
}
}