integrasi php ci dan smarty

Iseng-iseng nyoba ngoprek salah satu framework PHP yaitu CI. Tetapi koq merasa kurang sreg di sisi untuk menampilkan data karena dari contoh-contoh yang ada biasanya masih menggunakan scriptlet (hehe JAVA-nya keluar). Karena terbiasa bermain dengan JSTL, maka aku ingin mencari padanannya di PHP. Setelah bertanya ke mbah google, akhirnya sampai lah kesimpulanku untuk menggabungkan antara CI dan Smarty.

Pada praktikum kali ini menggunakan LAMPP, CI 2.1.0, Smarty 3.1.7, sedangkan syntax PHPnya menggunakan PHP 5.0 (bener gak ya). Untuk cara installasi LAMPP bisa dilihat disini. Untuk memulai praktikum ini langkah-langkah yang harus dilakukan adalah sebagai berikut:
1. Extract CI kemudian rename direktori menjadi “contoh” lalu letakkan di LAMPP_HOME/htdocs/.

2. Extract Smarty kemudian rename direktori menjadi “smarty” lalu letakkan di CI_HOME/application/third_party/. Hapus semua file yang ada di direktori smarty kecuali direktori libs. Di dalam direktori smarty tersebut buat direktori berikut: cache, configs, templates_c.

3. Buat file mysmarty.php di CI_HOME/application/libraries/ dimana isi dari file tersebut adalah:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
require_once (APPPATH . 'third_party/smarty/libs/Smarty.class.php');
class Mysmarty extends Smarty {
	function __construct(){
		parent::__construct();
		$SMARTY_HOME = APPPATH . "third_party/smarty/";
		$this->template_dir = APPPATH . "views/";
		$this->config_dir = $SMARTY_HOME . "configs/";
		$this->compile_dir = $SMARTY_HOME . "templates_c/";
		$this->cache_dir = $SMARTY_HOME . "cache/";
		$this->caching = 0;
	}
}
?>

4. Buat file MY_Controller.php di CI_HOME/application/core/ dimana isi dari file tersebut adalah:

<?php (defined('BASEPATH')) OR exit('No direct script access allowed');
class MY_Controller extends CI_Controller{
	public $smarty;
	function __construct(){
		parent::__construct();
	}

	function put($key, $value = NULL){
		if($key != NULL){
			$this->mysmarty->assign($key, $value);
		}
	}

	function view($template){
		$this->mysmarty->display($template. '.tpl');
	}

	function cekInstall(){
		$this->mysmarty->testInstall();
	}

}
?>

5. Buat file home.php di CI_HOME/application/controllers/ dimana isi dari file tersebut adalah:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends MY_Controller{
	public function __construct(){
		parent::__construct();
	}

	public function index(){
		//$this->cekInstall();
		$this->put('description', 'deskripsi');
		$this->put('title', 'judul');
		$this->view('home');
	}
}
?>

6. Buat file home.tpl di CI_HOME/application/views/ dimana isi dari file tersebut adalah:

<html>
	<head>
		<title>Info</title>
	</head>
	<body>
		<u>Test Article</u><br/>
		Title: {$title}<br/>
		Description: {$description}<br/>
	</body>
</html>

7. Edit file autoload.php yang ada di CI_HOME/application/config/ di bagian libraries sehingga menjadi seperti berikut:

$autoload['libraries'] = array('mysmarty');

8. Langkah berikutnya dilakukan jika installasi dilakukan di Linux (Untuk Windows kyaknya gak perlu). Ketik perintah berikut di terminal

sudo chown nobody:hape /opt/lampp/htdocs/contoh/application/third_party/smarty/templates_c/
sudo chown nobody:hape /opt/lampp/htdocs/contoh/application/third_party/smarty/cache/

dimana hape adalah username yang digunakan saat login ke linux.
9. Lakukan test installasi dengan uncomment perintah cekInstall pada file home.php sedangkan baris dibawahnya di comment saja. Setelah itu ketikkan perintah berikut di browser http://localhost/contoh/index.php/home sehingga browser akan menampilkan hasil berikut.

Smarty Installation test...
Testing template directory...
/opt/lampp/htdocs/contoh/application/views is OK.
Testing compile directory...
/opt/lampp/htdocs/contoh/application/third_party/smarty/templates_c is OK.
Testing plugins directory...
/opt/lampp/htdocs/contoh/application/third_party/smarty/libs/plugins is OK.
Testing cache directory...
/opt/lampp/htdocs/contoh/application/third_party/smarty/cache is OK.
Testing configs directory...
/opt/lampp/htdocs/contoh/application/third_party/smarty/configs is OK.
Testing sysplugin files...
... OK
Testing plugin files...
... OK
Tests complete.

10. Jika test berhasil, comment kembali perintah cekInstall yang ada di langkah 9 dan uncomment perintah-perintah dibawahnya. Buka di browser seperti pada langkah 9 maka akan dihasilkan tampilan berikut.

Test Article
Title: judul
Description: deskripsi

Sekian tulisan dari saya mudah-mudahan bisa membantu, jika ada yang masih kurang tepat mohon sarannya. Terima Kasih.

Note:
LAMPP_HOME: direktori tempat LAMPP di install (/opt/lampp)
CI_HOME: direktori tempat CI di install (/opt/lampp/htdocs/contoh)

Kata Kunci: integrasi CI Smarty, menggabungkan CI dan Smarty, CI Smarty, Framework Smarty

Sumber:
http://codesamplez.com.

Leave a Reply