Using Smarty templates with CodeIgniter
Huy Châu - March 25, 2013 -
Using Smarty templates with CodeIgniter
- Download the Smarty library Zip file and copy the Smarty directory from the Zip into your Code Igniter project’s /application/third_party folder. You should now have a directory along the lines of /application/third_party/Smarty-3.1.8/ in your project.
- Rename folder Smarty-3.1.8 to smarty.
- Create a new PHP file in the /application/libraries/ folder called mysmarty.php.
- Paste the following into mysmarty.php:
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
require_once(APPPATH.'third_party/smarty/libs/Smarty.class.php');
/**
* MySmarty Class
*
* initializes basic smarty settings and act as smarty object
*
* @final Mysmarty
* @category Libraries
* @author Md. Ali Ahsan Rana
* @link http://codesamplez.com/
*/
class Mysmarty extends Smarty
{
/**
* constructor
*/
function __construct()
{
parent::__construct();
$this->template_dir = APPPATH."/views/";
$this->config_dir = APPPATH."/conf/";
$this->compile_dir = APPPATH."/cache/";
$this->caching = 0;
}
}
?>
- Create Smarty .tpl files in your /application/views/ directory instead of standard CodeIgniter views. For example, for the home page you would create a home.tpl file in /views instead of home.php.
- Finally, add mysmarty in autoload script. In your Controller classes you need to load your new mysmarty library and tell it to render a .tpl file using some data. The data will probably have been loaded by your Controller from a Model. Here’s an example for a home page:
public function index()
{
$this->mysmarty->assign('title','Test Title');
$this->mysmarty->assign('description','Test Description');
$this->mysmarty->display('home.tpl');
//$this->load->view('welcome_message');
}
In template view (home.tpl), type this:
<html>
<head>
<title>{$title}</title>
</head>
<body>
{$description}
</body>
</html>
Done !Thanks Steve Claridge, for your great posts
Source:

