February 15, 2005

PHP template engine.

Here is my 26 line PHP template engine that I have used for dozens of little web applications. It's clean, fast and mean. The templates are just PHP files. Nothing weird or strange about them.

Template class

class std_TemplateEngine{

   var $data;
   var $useHeaderFooter = true;
   
   function std_TemplateEngine(){
      $this->data=array();
   }
   
   function assign($name,$item){
      $this->data[$name]=$item;
   }
   
   function assignRef($name,&$item){
      $this->data[$name] =& $item;
   }
   
   function display($templateName){
      extract($this->data);
      include("templates/".$templateName.".html.php");
   }
   
   function displayPage($templateName){
      if($this->useHeaderFooter){
        $this->display("pageHeader");
        $this->display($templateName);
        $this->display("pageFooter");
        }else{
            $this->display($templateName);
        }
   }
}


Example

Controller code

<?
$t = new std_TemplateEngine();
$t->assign("title","Chunky Bacon");
$t->assign("lucky_numbers",[1,2,7]);

$t->display("document_view");
?>

Template code (templates/document_view.html.php)

<h1><?=$title?></h1>

<ul>
<? foreach($lucky_numbers AS $number){ ?>
   <li><?=$number?></li>
<? } ?>
</ul>


Posted by Daniel at 11:32 PM | Comments (0)

February 12, 2005

My neck of the woods.

So I was testing out a locations specfic search engine, and this was the news item that came up: S.C. Teen Arrested for Spitting in Coffee.

Posted by Daniel at 06:18 PM | Comments (0)

February 07, 2005

Consulting.

The best overview of real life freelance development I've read: So you want to be a consultant?.

Posted by Daniel at 01:20 PM | Comments (1)