Why CodeIgniter? Because it’s easy to use, fast, flexible and yet powerful and well documented MVC framework.
Why Smarty? Smarty is an advanced template engine and has many advantages over the CodeIgniter’s default templating system:
- helps me keep the views much cleaner; I realy don’t like having tags among the html code. I’ve noticed these tags can encourage some developers to include in the view code what should be in the controller.
- keeps a better visual separation between dynamic and static content
- compiles files into pure and fast php code,
- has it’s own cache system
- has it’s own helpers (functions, modifiers)
Integration steps:
1. The first step would be get and unpack latest Smarty (http://www.smarty.net/) to application/libraries/smarty/ folder. Of course, you already have CodeIgniter (http://codeigniter.com/downloads/) installed.
2. Create Directories:
application/cache/smarty
application/cache/smarty/templatetes_c 0777
application/cache/smarty/cache 0777
application/cache/smarty/configs
3. Create a CI configuration file for Smarty:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// Please see Smarty user guide for more info: http://smarty.php.net/manual/en/api.variables.php
$config['smarty_templates'] = APPPATH . 'views/';// CodeIgniter's views folder will be used as templates folder for Smarty
$config['smarty_templates_c'] = APPPATH . 'cache/smarty/templates_c/';
$config['smarty_configs'] = APPPATH . 'cache/smarty/configs/';
$config['smarty_cache'] = APPPATH . 'cache/smarty/cache/';
$config['smarty_caching'] = 1;
$config['cache_lifetime'] = 3600;
$config['smarty_force_compile'] = true;
$config['smarty_compile_check'] = true;
$config['smarty_template_ext'] = '.tpl';
$config['smarty_debugging'] = true;
4. Make appropiate changes to the application/config/autoload.php file:
- Find $autoload['libraries'] = line and add ‘MY_Smarty’ there
- Find $autoload['config'] = line and add ‘smarty’ there
5. Create application/libraries/MY_Smarty.php file:
if (!defined('BASEPATH')) exit('No direct script access allowed');
require "smarty/Smarty.class.php";
class MY_Smarty extends Smarty
{
function MY_Smarty()
{
parent::__construct();
$config =& get_config();
$this->template_dir = $config['smarty_templates'];
$this->compile_dir = $config['smarty_templates_c'];
$this->config_dir = $config['smarty_configs'];
$this->cache_dir = $config['smarty_cache'];
$this->debugging = $config['smarty_debugging'];
$this->caching = $config['smarty_caching'];
$this->force_cache = $config['smarty_force_compile'];
$this->force_compile = $config['smarty_compile_check'];
$this->cache_lifetime = $config['cache_lifetime'];
}
function view($resource_name, $params = array()) {
$config =& get_config();
if (strpos($resource_name, '.') === false) {
$resource_name .= $config['smarty_template_ext'];
}
if (is_array($params) && count($params)) {
foreach ($params as $key => $value) {
$this->assign($key, $value);
}
}
if (!is_file($this->template_dir . $resource_name)) {
show_error("template: [$this->template_dir$resource_name] cannot be found.");
}
return parent::display($resource_name);
}
}
6. Usage
In your controller:
function test_smarty(){
$this->my_smarty->assign('variable', 'value');// send variable
$this->my_smarty->view('index');// display selected template
}
// vs
function test_default(){
$data['variable'] = 'value'; // send the variable
$this->load->view('index', $data); // load the page
}
In your view:
{$variable}
<?php echo $variable;?>