Location: PHPKode > scripts > AHM Template Engine > ahm.template.engine.php
<?
#########################################################################################
#
#
#
#                            AHM Template Engine
#                                 Version: 1.0
#                            Release: April 16, 2008
#                  Developed By: A.H.M. Shahnur Alam. 'Shaon'
#                        Developer, IXXO Internet Solution, Germany.                       
#                            Support: hide@address.com
#                                Mobile: +8801713129363
#  
#
#########################################################################################

class myTemplate
{
 /* Starting Tag Of a variable */
 var $STAG="#";
 
 /* Prefix Of a variable */
 var $PREFIX='$';
 
 /* Ending Tag Of a variable */
 var $ETAG="#";
 
 /* Variable Structure {VARNAME} */
 var $ahm_tpl_var;
 
 /* Count Variable */
 var $ahm_count_var=0;

 
 /* Keep All Variables */
 var $ahm_all_vars=array();
 
 
 var $repeat="/#repeat foreach \$(.*) as \@(.*)#(*.)#xrepeat#/";


 var $tplpath="";
 
 
 var $tplname="";


 /************************************************************************
  *
  *   Set The Path For Template
  *
  ************************************************************************/
 function setTplpath($path)
  {
    $this->tplpath=$path;
  }
 
 
 /************************************************************************
  *
  *   > Define Regular Expression
  *   > Get All GET/POST variable
  *
  ************************************************************************/
 function myTemplate()
  {
   $this->ahm_tpl_var="/".$this->STAG."\\".$this->PREFIX."([\S]*)".$this->ETAG."/";  //"
   $this->getAll_GET();
   $this->getAll_POST();
  }

 /************************************************************************
  *
  *   get all get vars
  *
  ************************************************************************/
 function getAll_GET()
  {
   $names=array_keys($_GET);
   for($i=0;$i<count($names);$i++)
    {
     $this->ahm_all_vars[$this->ahm_count_var]['name']=$this->STAG.$this->PREFIX.'get.'.$names[$i].$this->ETAG;;
     $this->ahm_all_vars[$this->ahm_count_var]['value']=$_GET[$names[$i]];
     $this->ahm_count_var=$this->ahm_count_var+1;
    }
  }
  
 /************************************************************************
  *
  *   get all post vars
  *
  ************************************************************************/
  function getAll_POST()
  {
   $names=array_keys($_POST);
   for($i=0;$i<count($names);$i++)
    {
     $this->ahm_all_vars[$this->ahm_count_var]['name']=$this->STAG.$this->PREFIX.'post.'.$names[$i].$this->ETAG;
     $this->ahm_all_vars[$this->ahm_count_var]['value']=$_POST[$names[$i]];
     $this->ahm_count_var=$this->ahm_count_var+1;
    }
  }

  
 /************************************************************************
  *
  *   Assign a Variable
  *   This is save in a array
  *   variable pattern /#$(.*)#/
  *
  ************************************************************************/
 function setVar($name,$value)
  {
   if(!is_array($value))
    {
      $this->ahm_all_vars[$this->ahm_count_var]['name']=$this->STAG.$this->PREFIX.$name.$this->ETAG;
      $this->ahm_all_vars[$this->ahm_count_var]['value']=$value;
    }
   else
    {
       $keys=array_keys($value);
       $this->ahm_all_vars['$'.$name.'.count'] = count($value);
       for($i=0;$i<count($keys);$i++) {
       if(is_array($value[$keys[$i]])) {
        $_keys=array_keys($value[$keys[$i]]);
        for($j=0;$j<count($_keys);$j++) {
         if(is_array($value[$keys[$i]][$_keys[$j]])) {
          $__keys=array_keys($value[$keys[$i]][$_keys[$j]]);
            for($k=0;$k<count($__keys);$k++) {
            $tmp=$name.".".$keys[$i].".".$_keys[$j].".".$__keys[$k];
            $this->ahm_all_vars[$tmp]=$value[$keys[$i]][$_keys[$j]][$__keys[$k]];
            }
           }
           else {
            $tmp=$name.".".$keys[$i].".".$_keys[$j];
            $this->ahm_all_vars[$tmp]=$value[$keys[$i]][$_keys[$j]];
          }
         }
        }
        else {
         $tmp=$name.".".$keys[$i];
         $this->ahm_all_vars[$tmp]=$value[$keys[$i]];
        }
       }
    }
   $this->ahm_count_var=$this->ahm_count_var+1;
  }
  
  
 /************************************************************************
  *
  *   Display The Template
  *
  ************************************************************************/
 function display($tplname)
  {
   if(!empty($this->tplpath)) $this->tplname = $this->tplpath . "/" . $tplname;
   else $this->tplname = $tplname;
   
   if(file_exists($this->tplname)) /*  Check The Existence Of File  */
         $rawcontent = file_get_contents($this->tplname);
   else
         /*  Show The Error If File Not Found  */
         $rawcontent="<center><font face=verdana style='font-size: 8pt' color=red><b>Error :</b> File Not Found [$this->tplname] </font></center>";
   
   $compiled_contents=$this->Compile($rawcontent);
   
   print_r($this->all_tpl_vars);
   echo $compiled_contents;
  }


 /************************************************************************
  *
  *   Compile The Template
  *
  ************************************************************************/
 function Compile($contents)
  {

   $repeat_count=$this->CountRepeat($contents);

   for($i=0;$i<$repeat_count;$i++)
   $contents=$this->Repeat($contents);


   for($i=0;$i<$this->ahm_count_var;$i++)
    {
      $contents=str_replace($this->ahm_all_vars[$i]['name'],  // Replace Vriable
                            $this->ahm_all_vars[$i]['value'], // With Value
                            $contents);
    }


    $contents=preg_replace($this->ahm_tpl_var,"",$contents);
    
    
    return $contents;
  }

/************************************************************************************
 *
 * Count The Total Numbers Of Repeat Statement In The Contents
 *
 *
 ***********************************************************************************/
 function CountRepeat($contents)
   {
     preg_match_all("/#repeat/", $contents, $matches);
     return count($matches[0]);
   }

/************************************************************************************
 *
 * Process Repeat Statement
 *
 *
 ***********************************************************************************/
 function Repeat($contents)
  {
   $start = strpos($contents,"#repeat");
   $end = strpos($contents,"#xrepeat#");
   $loop=substr($contents,$start,$end-$start);
   $rstr=substr($contents,$start,$end-$start+9);
   $loop=explode("\r\n",$loop);
   preg_match_all("#(\s*)repeat(\s+)\[limit=(.*)\](\s+)\[interval=(.*)\](\s+)\[index=(.*)\](\s*)#",$loop[0],$loop[0],PREG_SET_ORDER);
   for($i=1;$i<count($loop);$i++)
   $code.=$loop[$i];
   $loop['code']=$code;
   
   preg_match_all("/#\\$([\w]*)((\.[\w]+)+)#/",$loop[1],$loop[1],PREG_SET_ORDER);
   $loop['limit']=$loop[0][0][3];
   if(!is_numeric(trim($loop['limit']))){
   $loop['limit'] = $this->ahm_all_vars[$loop['limit']];
   }
   //else echo $loop['limit'];
   //echo $loop['limit'];
   $loop['interval']=$loop[0][0][5];
   $loop['index']=$loop[0][0][7];
   $tmp=$loop['code'];   
   
    
   
   for($i=0;$i<$loop['limit'];$i+=$loop['interval'])
    {
     if(count($loop[1])>0) {
     $tmp2=$tmp;
     for($j=0;$j<count($loop[1]);$j++)
      {
         $v=str_replace('.'.$loop['index'],'.'.$i,$loop[1][$j][1].$loop[1][$j][2]);
         $name = $v;
         $n = $loop[1][0][0];
          
         $value=$this->ahm_all_vars[$v];         
         $tmp2=str_replace($n,$value,$tmp2);
      }
      $tmp1.=$tmp2;
     } else {
       $tmp1.=$tmp;
     }

    }
   $contents=str_replace($rstr,$tmp1,$contents);
   return $contents;
  }
/*************************************************************************************/

}



/*************************************************************************
 *
 *
 *
 *
 *
 *
 *
 *
 ************************************************************************/
class AHMTemplate
  {

 /****************************************************************************
  *
  *  Start Template Engine
  *
  ****************************************************************************/
    function Start()
     {
      global $tpl;
      $tpl=new myTemplate();
     }

 /****************************************************************************
  *
  *
  *
  ****************************************************************************/
    function assign($name,$value)
     {
       global $tpl;
       if(empty($tpl))  AHMTemplate::Start();

       $tpl->setVar($name,$value);
     }
     
 /****************************************************************************
  *
  *
  *
  ****************************************************************************/
    function setTplDir($dir)
     {
       global $tpl;
       if(empty($tpl))  AHMTemplate::Start();

       $tpl->setTplpath($dir);
     }

 /****************************************************************************
  *
  *
  *
  ****************************************************************************/
    function display($tplname)
     {
       global $tpl;
       if(empty($tpl))  AHMTemplate::Start();

       $tpl->display($tplname);
     }

     
  }
  

//AHMTemplate::assign("user","SHAON");
//AHMTemplate::display("a.tpl");
?>
Return current item: AHM Template Engine