As PHP application become more and more complex, that makes maintenance nearly very hard . One of the main design pattern is MVC (model- view-controller), which helps in separating application logic from view. Typically, the application code contains the business logic of your application, written and maintained in PHP code. This code is maintained by programmers. The presentation is the way your content is presented to the end user, which is written and maintained in template files. The templates are maintained by template designers .

Here I am going to show you how to create a simple template class , which is hack of logic behind CodeIgniter for fetching a particular view .

Our basic template class has the following basic methods.
class Template{
var $tpldir ='templates';
var $data = array();
var $_path ;
function setTplDir ($path ){}
function assign ($key , $val ){}
function fetch ($path ){}
function setTplPath($path){}
function display($path){}
}
First we have to set our template directory . ’setdir’ function will set template path , so we can place our templates in that directory.
function setTplDir($path ){
$this->tpldir = $path ;
}
Assign function is used to set template variables . Template variables are passed as a key value pair
function assign ($key , $val ){
$this->data[$key] = $val;
}
Our main template function is ‘fetch’ . setTplPath set the path for template, separated for later addition to folder and relative path checking . After including the template ,its buffered output will be returned.
function fetch ($path ){
extract($this->data);
$this->setTplPath($path);
ob_start();
include($this->_path);
$buffer = ob_get_contents();
@ob_end_clean();
return $buffer;
}
Display function print the output from fetch function .
Template class :
class Template{
var $tpldir ='templates';
var $data = array();
var $_path ;
function setTplDir($path ){
$this->tpldir = $path ;
}
function assign ($key , $val ){
$this->data[$key] = $val;
}
function fetch ($path ){
extract($this->data);
$this->setTplPath($path);
ob_start();
include($this->_path);
$buffer = ob_get_contents();
@ob_end_clean();
return $buffer;
}
function setTplPath($path){
$this->_path = $this->tpldir.'/'.$path.'.php';
}
function display($path){
echo $this->fetch($path);
}
}
Example code using our template class . index.php
$tplobj = new Template;
$tplobj->setTplDir('views');
$students = array('raju', 'renjith', 'arun', 'soumya');
$tplobj->assign('students',$students);
$tplobj->display('studlist');
View code :
<ul> <?php foreach($students as $stud): ?> <li> <?php echo $stud; ?></li> <?php endforeach ; ?> </ul>

Hi, I can’t understand how to add your site in my rss reader. Can you Help me, please
Great article, thank you very much!
I am the first time on this site and am really enthusiastic about and so many good articles. I think it’s just very good.
Thank you for Posting & I got to read nice information on your site.
Great article, I especially like what you had to say in the last paragraph.
…
This article may possibly be described on Twitter by somebody. genuinely?…
I have to state, you chose your words well. The ideas you wrote on your encounters are well placed. This is an incredible blog!
…
I admire the important facts you offer within your articles. I’ll bookmark your blog site and have my kids check up right here usually. I’m rather confident they’ll find out a lot of new stuff the following than anybody else!…
Hi, I’m very interested in Linux but Im a Super Newbie and I’m having trouble deciding on the right distribution for me (Havent you heard this a million times?) anyway here is my problem, I need a distribution that can switch between reading and writing in English and Japanese (Japanese Language Support) with out restarting the operating system.
Spitze Design hat dieser Blog. Woher hast du die Vorlage ? War bestimmt sehr teuer.