Nah, kali ini saya mau sedikit menjelaskan langkah-langkah menerapkan Smarty di framework Codeigniter sebagai berikut :
- Setelah Codeigniter terpasang, buatlah folder dengan nama Smarty di folder system/libraries/
- Download Smarty dan ekstrak di system/libraries/Smarty/ yang tadi dibuat.
- Buatlah file dengan nama Smarty.php dan simpan di system/libraries/ , kemudian edit file tersebut dan simpan kode berikut, lalu save filenya
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); require_once( BASEPATH.'libraries/Smarty/libs/Smarty.class.php' ); class CI_Smarty extends Smarty { public function __construct() { parent::__construct(); $this->compile_dir = APPPATH . "views/templates_c"; $this->template_dir = APPPATH . "views/templates"; $this->assign( 'APPPATH', APPPATH ); $this->assign( 'BASEPATH', BASEPATH ); // Assign CodeIgniter object by reference to CI if ( method_exists( $this, 'assignByRef') ) { $ci =& get_instance(); $this->assignByRef("ci", $ci); } log_message('debug', "Smarty Class Initialized"); } function view($template, $data = array(), $return = FALSE) { foreach ($data as $key => $val) { $this->assign($key, $val); } if ($return == FALSE) { $CI =& get_instance(); if (method_exists( $CI->output, 'set_output' )) { $CI->output->set_output( $this->fetch($template) ); } else { $CI->output->final_output = $this->fetch($template); } return; } else { return $this->fetch($template); } } } - Kemudian buatlah dua folder dengan nama templates dan templates_c di folder application/views/
- Buatlah file template misalnya test.tpl dan simpan di folder application/views/templates/ isi file tersebut : <html><head></head><body>{$title}</body></html>
- Lakukan test di controller Anda dengan kode kurang lebih seperti berikut :
public function index()
{
$data['title'] = 'hello world';
$this->smarty->view('test.tpl',$data);
}
No comments:
Post a Comment