Dec 282011
 

In this post I will show you how to integrate Smarty 3 (I use the current version Smarty 3.1.3 ) with CodeIgniter 2 (2.0.3).

1. Create CodeIgniter application ready to integrate Smarty

Create a folder name codeigniter-smarty and copy codeigniter lib folder to it. You will make something like this.

codeigniter smarty integration thumb CodeIgniter 2 Smarty 3 integration

2. Download Smarty and prepare library for CodeIgniter

Now you will need to download Smarty and copy its libs folder.

smarty lib for codeigniter thumb CodeIgniter 2 Smarty 3 integration

In your CodeIgniter application folder, find the libraries folder and create a folder name “smarty” and a file name smarty.php. Don’t worry about their content, we will complete it softly, now just paste your libs folder copy from Smarty to libraries > smarty folder

codeigniter smarty integration folder thumb CodeIgniter 2 Smarty 3 integration

You will also need a cache and a config folder, please create them in the librariessmarty as following:

image thumb3 CodeIgniter 2 Smarty 3 integration

3. Smarty and CodeIgniter Integration

Now you 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)

<?php 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()
	{
		date_default_timezone_set('America/Phoenix');

		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 );
		// $this->caching = Smarty::CACHING_LIFETIME_CURRENT; // Does something icon smile CodeIgniter 2 Smarty 3 integration 
		if ( method_exists( $this, 'assignByRef') )
		{
			$ci =& get_instance();
			$this->assignByRef("ci", $ci);
		}
		$this->force_compile = 1;
		$this->caching = true;
		$this->cache_lifetime = 120;

		//log_message('debug', "Smarty Class Initialized");
	}

	function view($template_name) {
		if (strpos($template_name, '.') === FALSE && strpos($template_name, ':') === FALSE) {
			$template_name .= '.tpl';
		}
		parent::display($template_name);
	}

}

?>

You 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', 'session', 'pagination', 'smarty');

OK. that must be fine now. We will run a test soon.

4. Test Smarty and CodeIgniter integration.

Here is the final step, you need to test the correctness of the integration. I won’t reinvent the wheel but use the example of smarty. Please find in the Smartydemo folder a file name index.php, here is its content.

<?php
 /**
 * Example Application

 * @package Example-application
 */

require('../libs/Smarty.class.php');

$smarty = new Smarty;

//$smarty->force_compile = true;
$smarty->debugging = true;
$smarty->caching = true;
$smarty->cache_lifetime = 120;

$smarty->assign("Name","Fred Irving Johnathan Bradley Peppergill",true);
$smarty->assign("FirstName",array("John","Mary","James","Henry"));
$smarty->assign("LastName",array("Doe","Smith","Johnson","Case"));
$smarty->assign("Class",array(array("A","B","C","D"), array("E", "F", "G", "H"),
	  array("I", "J", "K", "L"), array("M", "N", "O", "P")));

$smarty->assign("contacts", array(array("phone" => "1", "fax" => "2", "cell" => "3"),
	  array("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234")));

$smarty->assign("option_values", array("NY","NE","KS","IA","OK","TX"));
$smarty->assign("option_output", array("New York","Nebraska","Kansas","Iowa","Oklahoma","Texas"));
$smarty->assign("option_selected", "NE");

$smarty->display('index.tpl');
?>

OK I will turn it into a CodeIgniter Controller so you can run it now. In the controller folder of CodeIgniter application folder,  create a file name testsmarty.php as following:

<?php
class Testsmarty extends CI_Controller {

	function __construct() {
		parent::__construct();
	}

	function index() {
		$this->smarty->assign("Name","Fred Irving Johnathan Bradley Peppergill",true);
		$this->smarty->assign("FirstName",array("John","Mary","James","Henry"));
		$this->smarty->assign("LastName",array("Doe","Smith","Johnson","Case"));
		$this->smarty->assign("Class",array(array("A","B","C","D"), array("E", "F", "G", "H"),
		array("I", "J", "K", "L"), array("M", "N", "O", "P")));

		$this->smarty->assign("contacts", array(array("phone" => "1", "fax" => "2", "cell" => "3"),
		array("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234")));

		$this->smarty->assign("option_values", array("NY","NE","KS","IA","OK","TX"));
		$this->smarty->assign("option_output", array("New York","Nebraska","Kansas","Iowa","Oklahoma","Texas"));
		$this->smarty->assign("option_selected", "NE");	

		$this->smarty->view('index');

	}
}

And here is the result of the integrating:

image thumb4 CodeIgniter 2 Smarty 3 integration

5. Download the source code.

You can download the whole integration then just change some codeigniter config and ready to run. Use svn check out at the following address:

https://frameworks-integration.googlecode.com/svn/trunk/codeigniter-smarty/codeigniter2-smarty3

  11 Responses to “CodeIgniter 2 Smarty 3 integration”

  1. Hi i followed the instructions and worked Thanks

  2. Such a great guide, work like a charm. Thanks.

  3. On point 4. wher does the index.php file reside in the application, is it the config, cache or libs folder under smarty folder? as where ever i put it the application doesn’t work.

    i am at a bit of a loss now.

    Thanks

  4. ok i am a moron, i didn’t add the compiled folder in the views folder and th index.phph file is in fact converted to be the testsmarty.php file in the controller folder.

    **note to self** read the tutorial properly first :o /

  5. Great guide and it is working perfectly. Thanks!

  6. Hi,

    Thanks for sharing. Great article and easy to understand.

  7. THX! helped me alot!

  8. svn link is great. Except that I get a “Unable to connect to your database server using the provided settings” error regardless of settings in system\database\DB_driver.php. No useful troubleshooting info appears to be available.

  9. Hi,

    Thanks for this solution, because for CI 2.1 and Smarty 3 it is working with multiple cache and compiled file.
    But i have a question, when i load the profiler, the DB query are executed even if the cache is actived !
    Have you a reason or a solution ?


    public function index() {
    $my_cache_id="page".$_GET['page'];
    $this->smarty->caching = 2;
    $this->smarty->cache_lifetime = 3600;

    if(!$this->smarty->isCached('test2',$my_cache_id)) {
    $this->load->model('fondecran_model');
    ....
    $this->smarty->assign("query",$this->fondecran_model->get_fondecran($pagination->byPage, $from));
    ....
    $this->output->enable_profiler(true);
    }
    $this->smarty->view('test2', $my_cache_id);
    }

    Thanks for all and happy new years !

  10. Great article I used and it work fine. Thanks

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>