
Before, I wrote tutorials about smarty basics as well as codeigniter tutorial for beginners. As the ci framework doesn’t include any template engine, many developers love to use Smarty with codeigniter-based applications as the view engine. In this tutorial, I will try to guide you through how to integrate the Smarty template engine library in the Codeigniter framework and how we can use it most efficiently. I am assuming you have some basic knowledge of both Smarty and Codeigniter; if not, I recommend reading about their basics first. You are going to love the power of the CodeIgniter smarty pair!
Read The Complete CodeIgniter Tutorials Series By CodeSamplez.com
Download and install:
First, we need to have the latest version of CodeIgniter and Smarty library. First, unzip and place the codeginiter files in your local ‘htdocs’ directory. Then unzip the smarty library and place the ‘smarty’ directory inside the codeigniter’s ‘application/third_party’ directory. Now, we can reuse the ‘mysmarty’ class that we used on the Smarty basic tutorial. Place that in the ‘application/libraries’ directory. Now, as we will need this library every time a page is loaded, it’s good to load our library with an auto-load option. So, add ‘mysmarty’ to the autoload libraries array in ‘application/config/autoload.php’ file.
Usage Example:
Now, we just have to assign values to smarty variables and call the display method using the proper template file name. Following is a sample code snippet of a CodeIgniter controller class:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* @property CI_Loader $load
* @property CI_Form_validation $form_validation
* @property CI_Input $input
* @property CI_Email $email
* @property Mysmarty $mysmarty
*/
class Home extends MY_Controller
{
/**
* constructor
*/
public function __construct()
{
parent::__construct();
}
/**
* Default function that will be executed unless another method specified
*/
public function index()
{
// basic assignment for passing data to template file
$smarty->assign('title', 'Test Title');
$smarty->assign('description', 'Test Description');
// show the template
$smarty->display('index.tpl');
}
Code language: HTML, XML (xml)
Here is the sample template file that is located in the ‘application/views’ directory.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Smarty Test Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<div class="container" id="container">
<h2>{$title}</h2>
<p>{$description}</p>
</div>
</body>
</html>
Code language: HTML, XML (xml)
Codeigniter Smarty View Transformation/Migration:
It’s a general case that you might have already started with built-in codeigniter views and are now getting interested in using Smarty as the view. Sure, you can do this also, no doubt. Follow the following steps to meet such a goal. Also, even if you are starting a new application and have no issue with such migration, you should follow the following procedure as your application’s best practice, which will help you switch between the Smarty view and the Codeigniter view very easily with minimal effort.
Using a common ‘MY_Controller’: You should use a common MY_Controller for your CodeIgniter application that will inherit core ‘CI_Controller’ and initialize all common functionality for all controllers. Also, every controller should inherit this ‘MY_Controller’ instead of the core controller. Following is a sample MY_Controller class(it should be on ‘application/core’ directory):
<?php (defined('BASEPATH')) OR exit('No direct script access allowed');
/**
* CodeIgniter MY_Controller Class
*
* initializes common controller settings, this is to be derived by all controllers of this application
*
* @name MY_Controller
* @category Core Libraries
* @author Md. Ali Ahsan Rana
* @link https://codesamplez.com/
*/
/**
* @property Mysmarty $mysmarty
*/
class MY_Controller extends CI_Controller
{
/**
* constructor
*/
function __construct()
{
parent::__construct();
}
/**
* final view codes for showing template
*/
function view($template,$data=NULL)
{
//assigns all data as smarty variables. Reduces smarty assignment in controllers
if($data != NULL)
{
foreach($data as $key => $value)
{
$this->mysmarty->assign($key, $value);
}
}
$this->mysmarty->display($template.".tpl");
}
}
Code language: HTML, XML (xml)
Convert controller methods: Now, you will have to convert the codes of controllers to apply the Smarty templates instead of Codeigniter views. Let’s assume you have a controller method as follows:
public function about()
{
$data["message"] = "About Smarty And Codeigniter";
return $this->load->view("about",$data);
}
Code language: PHP (php)
Now, assuming that the corresponding controller does inherit ‘My_Controller’ class, let’s just change the ‘$this->load->view(“about,” $data)’ to ‘$this->view(“about”,$data)’, that just removes the ‘load->’ part, will do the work we needed. This will trigger the ‘view’ method of the ‘My_Controller’ class that the controller inherited and will execute necessary smarty codes to do the rest. It’s pretty easy. 😀
Now, remember that if you have used the current format from the beginning, you can switch to any template mode anytime just by modifying the MY_Controller’s view method.
I hope this small tutorial will help you get started with using Smarty with the Codeigniter framework. If you are having some issues or have some inquiry, please let me know by commenting below, I will try to help as much as possible. Happy coding 🙂
Discover more from CODESAMPLEZ.COM
Subscribe to get the latest posts sent to your email.
very nice … going to implement it now … will see if any issues…thank you,
How can i include header.php/tpl and footer pages dynamically without creating assoc array? please suggest