Saturday, January 26, 2008

Integrating smarty in CI

Create one directory named ‘applications/init’.
create one file named ‘applications/init/init_mysmarty.php’
paste following in it:

if ( ! class_exists('MySmarty'))
{
require_once(APPPATH.'libraries/MySmarty'.EXT);
}

$obj =& get_instance();
$obj->mysmarty = new MySmarty();
$obj->ci_is_loaded[] = 'mysmarty';

?>

create one file named ‘applications/libraries/MySmarty.php’
paste following in the class :

class MySmarty extends Smarty{

function __construct()
{
$this->compile_dir = APPPATH . "views/templates_c";
$this->template_dir = APPPATH . "views/ templates";
log_message('debug', "Smarty Class Initialized");
}
}
?>

paste “applications/libraries/libs” of smarty in libraries of application : refer to MySmarty.php class for the path. Create ‘templates_c’ and ‘templates’ in views directory

Try this controller:

function Blog()
{
parent::Controller();
$this->load->library('mysmarty');
}
function index()
{

$this->mysmarty->assign('title',"Welcome");
$this->mysmarty->assign('content',"Hello!");
$smarty_array =array('one','two','three','four');
$this->mysmarty->assign('forloop',$smarty_array);
$this->mysmarty->display("basic.html");
}
}
?>
basic.html
{$title}{$content}


{section name=i loop=$forloop}
{$forloop[i]}


{/section}

No comments: