Sunny Books
What we have

Integrating Smarty and CodeIgniter

The original CodeIgniter framework does not separate PHP partnd HTML/CSS part, which soetimes makes the applications not clean and clear. Integrating Smarth template engine into CodeIgniter can completely separate the HTML/CSS presentation from PHP application logic while still takes advantage of MVC Model, View, and Controllers) of CodeIgniter.

The following shows how to integrate Smarty 3 and CodeIgniter 2.

Step 1: Download CodeIgniter and Smarty

Download CodeIgniter from here and Smarty from here.

Step2: Add Samrty into CodeIgniter libraries

1. In CodeIgniter application folder, find the libraries folder and create a folder name “smarty” and a file name smarty.php. The file smarty.php is for integration and we will discuss it very shortly.

2. Paste libs folder from Smarty to the folder just created: libraries -> smarty folder

3. Create another two folders in libraries -> smarty folder: cache folder and config folder. Now we have three folders in libraries -> smarty folder: cache, config, and smarty.

Step 3: Smarty and CodeIgniter Integration

Now we will need to fill the smarty.php in the libraries folder with this content. It will extends the Smarty class (Smarty.class.php) of smarty lib and create a CI_Smarty CodeIgniter standard library (ready to use in CodeIgniter control)

if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
require_once(APPPATH.'libraries/smarty/libs/Smarty.class.php');
 
class CI_Smarty extends Smarty {
 
    function __construct()
    {
        parent::__construct();
        $this->setTemplateDir(APPPATH.'views/templates');
        $this->setCompileDir(APPPATH.'views/compiled');
        $this->setConfigDir(APPPATH.'libraries/smarty/configs');
        $this->setCacheDir(APPPATH.'libraries/smarty/cache');
 
        $this->assign( 'APPPATH', APPPATH );
        $this->assign( 'BASEPATH', BASEPATH );
        if ( method_exists( $this, 'assignByRef') )
        {
            $ci =& get_instance();
            $this->assignByRef("ci", $ci);
        }
        $this->force_compile = 1;
        $this->caching = true;
        $this->cache_lifetime = 120;
 
    }
 
    function view($template_name) {
        if (strpos($template_name, '.') === FALSE && 
        strpos($template_name, ':') === FALSE) {
            $template_name .= '.tpl';
        }
        parent::display($template_name);
    }
}

Step 4: Cofigure CodeIgniter

We will also need to make CodeIgniter autoload the smarty library by changing the autoload.php of CodeIgniter as following

/*
| -------------------------------------------------------------------
|  Auto-load Libraries
| -------------------------------------------------------------------
| These are the classes located in the system/libraries folder
| or in your application/libraries folder.
|
| Prototype:
|
|   $autoload['libraries'] = array('database', 'session', 'xmlrpc');
*/
 
$autoload['libraries'] = array('database',  'smarty');

Step 4: Test integration

We need to create a php file in controllers folder and a tpl file in views folder. Then we use $this->smarty->assign to pass variable to the tpl file and $this->smarty->view to show tpl file.

the testsmarty.php file

class Testsmarty extends CI_Controller {
 
    function __construct() {
        parent::__construct();
    }
 
    function index() {
        $this->smarty->assign("title","Testing Smarty");
        $this->smarty->assign("description",
        	"This is the testing page for integrating Smarty and CodeIgniter.");
        $this->smarty->view('index');
 
    }
}

The index.tpl file

<p>index.tpl page shows the variables here:</p>
<p>title: {$title}</p>
<p>description: {$description}</p>
SUNWEB EXPERT