09 / 07 / 2009

smarty

ekleyen: Emre Çevik kategori: php| proje| smarty

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.

  • Digg
  • del.icio.us
  • Facebook
  • Google
  • BlinkList
  • blogmarks
  • description
  • Furl
  • Ma.gnolia
  • NewsVine
  • Reddit
  • Technorati

benzer konular:

  1. codeigniter kurulumu
  2. auth sınıfı
  3. auth sınıfı kullanımı
  4. class : codeigniter [ ekle / sil / güncelle ]
  5. parse sınıfı
  6. 3 katmanlı mimari ve php -2-


(1 votes, average: 5.00 out of 5)
Loading ... Loading ...

7 yorum

1 | enver

11 / 07 / 2009 - 13:13

Avatar

ci de smarty gibi güçlü bir tema motorunu kullanmak çok faydalı olacaktır. henüz benim için erken olabilir :) )

2 | php

12 / 07 / 2009 - 05:54

Avatar

merhaba,

verdiğiniz bilgiler için teşekkürler. konu ile hafif ilintili bir soru sormak istiyorum size,


$posta = new epostaGonder();
$posta->kime(”mail@mail.com”);
$posta->konu($_POST['konu']);
$posta->mesaj($smarty->fetch(’_img/txt/formlar.tpl’));
$posta->gonder();

yaptığımda aşağıdaki hatayı alıyorum ama dosya mevcut. sizce neden okumuyor?

Warning: Smarty error: unable to read resource: “home/public_html/_img/txt/formlar.tpl” in

3 | Emre Çevik

12 / 07 / 2009 - 13:46

Avatar

home/public_html/_img/txt/formlar.tpl dosyasının doğru dizinde olduğuna emin misin ?

4 | php

13 / 07 / 2009 - 08:19

Avatar

tesekkur ederim hallettim, template_dir ile ayni yerde olmasi gerekiyormus.

5 | omer

18 / 10 / 2009 - 15:22

Avatar

adam gelmis sonuna kadar cok güzel anlatmis son üc dört satir da suyu koyuvermis kim anlar aciklamasiz linkleri koymus örnegin

/project/sytem/application/views/main/index.tpl1 {$content}

/project/sytem/application/views/default/index.tpl1 smarty sistemi çalışıyor

http://localhost/project/index.php/ornek/index

adresine girdiğimizde ekran çıktısı

6 | omer

18 / 10 / 2009 - 16:19

Avatar

uzun ugraslardan sonra nihayet calisti

Theme.php de ki satir

function Output($tpl = ‘main.tpl’)
function Output($tpl = ‘index.tpl’) li olmali yoksa calismiyor.

7 | Emre Çevik

20 / 10 / 2009 - 01:34

Avatar

onlar açıklamasız linkler değil dosyalar ve içerikleri ayrıca düzeltme için teşekkürler..

yorum yaz

ne yapıyoruz

eğleniyoruz?

anket

design pattern yazılarını nasıl buldunuz?

sonuçlar

Loading ... Loading ...