projemizde codeigniter ile smarty kullanacağız. aslında codeigniter’in kendi template parseri var. fakat smarty kullanılması tema dosyalarının daha duzenli ve okunaklı olmasını sağlıyor veya bana öyle geliyor.
bu konuda codeigniter ile birlikte smarty’nin nasıl kullanılacağını anlatacağım.
önceki yazılarımızda codeigniter’i nasıl kuralacağınızı anlatmıştım.
smarty’nin sitesinden son sürümünü indiriyoruz. ben 2.6.26 sürümünü indirdim ve /libs klasörünü codeigniteri yuklediğimiz /project klasorunun içine yukluyoruz.
/project/cache
/project/cache/templates
/project/cache/templates/default
/project/sytem/application/views/default
/project/sytem/application/views/default/main
klasörlerini oluşturuyoruz
/project/sytem/application/config/config.php dosyasını açıyoruz ve
$config['theme'] = “default”;
satırını ekliyoruz.
/project/sytem/application/config/autoload.php dosyasını açıyoruz ve
$autoload['libraries'] = array();
satırını
$autoload['libraries'] = array(’theme’);
diye değiştiriyoruz.
şimdi tema sınıfımızı yapabiliriz.
/system/application/libraries/Theme.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); require 'libs/Smarty.class.php'; class Theme extends Smarty { function Theme() { log_message('debug', 'Smarty Library Initialized.'); $this->CI =& get_instance(); $this->tema = $this->CI->config->item('theme'); $this->Smarty(); $this->compile_dir = 'cache/templates/'.$this->tema.'/'; $this->template_dir = 'system/application/views/'.$this->tema.'/'; } function Output($tpl = 'index.tpl') { echo $this->fetch('main/'.$tpl); } } ?> |
tema sınıfımızı yaptık şimdi bir tane örnek yapalım.
/project/sytem/application/controllers/ornek.php
1 2 3 4 5 6 7 8 9 10 11 12 13 | class Ornek extends Controller { function Ornek() { parent::Controller(); } function index() { $this->theme->assign('content', $this->theme->fetch('index.tpl')); $this->theme->output(); } } |
/project/sytem/application/views/main/index.tpl
1 | {$content} |
/project/sytem/application/views/default/index.tpl
1 | smarty sistemi çalışıyor |
http://localhost/project/index.php/ornek/index
adresine girdiğimizde ekran çıktısı
smarty sistemi çalışıyor
smarty’nin dokumantasyonuna ulaşmak için tıklayın.
benzer konular:














design patterns