Sep
08
2011

CodeIgniter + SiXhEaD.Template

4,509 views

มาแล้วตามคำเรียกร้อง ตัวอย่างการใช้งานกับ CodeIgniter
บทความนี้ประกอบไปด้วย
- CodeIgniter v2.0.3
- SiXhEaD.Template v5.9

อธิบายขั้นตอนกันหน่อย

/application/libraries/
เอา class SiXhEaD.Template.php ไปใส่ใน directory นี้ โดยจะมี class my_template.php อีกตัวมาทำหน้าที่ extends class จาก SiXhEaD Template เพื่อใช้งานกับ CodeIgniter
- my_template.php
- SiXhEaD.Template.php

ไฟล์ my_template.php

 
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
 
require_once( APPPATH.'libraries/SiXhEaD.Template.php' );
 
class MY_Template extends Template {
 
    public function __construct($params)
    {
		$template_file	=	$params['template_file'];
		parent::__construct($template_file);
		$this->set_global(FALSE);
    }
}
 

/application/views/
วางไฟล์ template ต่างๆ (.html, .xml) โดย template นี้เอามาจาก demo ที่มากับชุด SiXhEaD.Template v5.9
- _tp_demo.index.html
- _tp_demo.simple.html
- _tp_demo.feeds.xml

/application/controllers/
- sixhead.php

ไฟล์ sixhead.php

 
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Sixhead extends CI_Controller {
 
	public function index()
	{
		$params = array('template_file' => APPPATH . 'views/_tp_demo.index.html');
		$this->load->library('my_template',$params);
 
		$this->my_template->block("data");
		for ($i=1;$i<=5;$i++) {
			$this->my_template->assign("i", $i);
			$this->my_template->apply();
		}
 
		$this->my_template->display();
	}
 
	public function simple()
	{
		$params = array('template_file' => APPPATH . 'views/_tp_demo.simple.html');
		$this->load->library('my_template',$params);
 
		$this->my_template->block("table1");
		for ($i=1;$i<=5;$i++) {
			$this->my_template->assign("column1", $i);
			$this->my_template->assign("column2", $i);
			$this->my_template->assign("column3", $i);
			$this->my_template->assign("column4", $i);
			$this->my_template->assign("column5", $i);
			$this->my_template->apply();
		}
 
		$DateTime = date("Y-m-d H:i:s");
		$this->my_template->assign("DateTime", $DateTime);
		$this->my_template->display();
	}
 
	public function feeds()
	{
		$params = array('template_file' => APPPATH . 'views/_tp_demo.feeds.xml');
		$this->load->library('my_template',$params);
 
		$this->my_template->block("items");
		for ($i=1;$i<=5;$i++) {
			$title		=	"Title $i";
			$description=	"Description $i";
			$url		=	"http://domain.com/php.php?Id=$i";
 
			$this->my_template->assign("title", $title);
			$this->my_template->assign("description", $description);
			$this->my_template->assign("url", $url);
			$this->my_template->apply();
		}
 
		header("Content-Type: text/xml; charset=UTF-8");
		$this->my_template->display();
	}
 
}
 

ตัวอย่าง 1
ตัวอย่าง 2
ตัวอย่าง 3

Download ทั้งชุด ได้ที่นี่


Written by Pipo in: Developer | Tags: , ,

No Comments »

RSS feed for comments on this post. TrackBack URL

Leave a comment

Page 1 of 11

Powered by WordPress | Aeros Theme | TheBuckmaker.com WordPress Themes