Location: PHPKode > scripts > Smart Engine Template Engine > smart-engine-template-engine/SmartEngine.php
<?php
/*
* Smart Engine Version 1.0
* Mohammad Sadegh Bagheri Ardabillian
* Hitron Soft LLC
* Copy Right 2005-2009 Hitron Soft LLC
*/

class SmartEngine{

    var $TemPath;
    var $elementvaluearray=array();
    var $debug=false;

    function File($file){
        if($this->CheckFile($file)){
            $source=$this->ReadFile($file);
            $source=$this->SearchFile($source);
            return $source;
        }
    }
    function SearchFile($Templatesource){
        preg_match_all("|{include (.*)}|US",$Templatesource,$includeelemets);
        if($includeelemets[0][0]){
            foreach ($includeelemets[1] as $key=>$value){
                $includefile=$includeelemets[0][$key];
                $source=$this->File("$value");
                $Templatesource=str_replace($includefile,$source,$Templatesource);
            }
        }
        preg_match_all('|{\$var (.*)}|Us', $Templatesource,$elements);
        preg_match_all('|{\$vararray (.*)}|Us', $Templatesource,$arrayelements);
        preg_match_all("|{foreach (.*)}(.*){/foreach}|Us",$Templatesource ,$foreachelements);
        preg_match_all("|{if (.*)}(.*){/if}|Us",$Templatesource ,$Ifelements);
        if(is_string($Ifelements[0][0])){
            foreach ($Ifelements[0] as $key=>$value){
                preg_match("|([a-zA-Z0-9]+)==([a-zA-Z0-9]+?)|Usi",$Ifelements[1][$key],$ifdata1);
                preg_match("|([a-zA-Z0-9]+)!==([a-zA-Z0-9]+?)|Usi",$Ifelements[1][$key],$ifdata2);
                if($ifdata1[1]){
                    if($this->elementvaluearray["constans"][$ifdata1[1]]==$ifdata1[2]){
                        $Templatesource=str_replace($value,$Ifelements[2][$key],$Templatesource);
                    }else{
                        $Templatesource=str_replace($value,"",$Templatesource);
                    }
                }else if($ifdata2[1]){
                    if($this->elementvaluearray["constans"][$ifdata2[1]]!==$ifdata2[2]){
                        $Templatesource=str_replace($value,$Ifelements[2][$key],$Templatesource);
                    }else{
                        $Templatesource=str_replace($value,"",$Templatesource);
                    }
                }
            }
        }
        if(is_string($foreachelements[0][0])){
            foreach ($foreachelements[0] as $key=>$value){
                if(is_array($this->elementvaluearray["array"][$foreachelements[1][$key]])){
                    foreach ($this->elementvaluearray["array"][$foreachelements[1][$key]] as $key2=>$value2){
                        $includesource=$foreachelements[2][$key];
                        //$value2 contain data as an array
                        if(is_array($value2)){
                            //replace array data with their places
                            foreach ($value2 as $key3=>$valuecomment){
                                $includesource=eregi_replace('{\$vararray '.$foreachelements[1][$key].".$key3}",$value2[$key3],$includesource);
                            }
                        }else{
                            //HS Error System
                            $this->Error("foreach_error",$value2,false);
                        }
                        $datasource.=$includesource;
                    }
                }else{
                    //HS Error System
                    $this->Error("foreach_error",$this->elementvaluearray["array"][$foreachelements[1][$key]],false);
                }
                $foreachsourcearray[$key]=$datasource;
                unset($datasource,$key,$value,$key2,$key3,$value2,$valuecomment);
            }
            foreach ($foreachsourcearray as $key=>$value){
                $Templatesource=str_replace($foreachelements[0][$key],$value,$Templatesource);
            }
        }
        unset($key,$value);
        foreach ($elements[0] as $key=>$value){
            $Templatesource=str_replace($value,$this->elementvaluearray["constans"][$elements[1][$key]],$Templatesource);
        }
        unset($key,$value);
        preg_match_all('|{time ([a-zA-Z0-9]+)\(?([a-zA-Z\-: ]+)?\)}|Usi', $Templatesource,$times);
        if($times[0][0]){
            foreach ($times[0] as $key=>$value){
                if($times[1][$key]=="now"){
                    if(($times[2][$key]=="null")or ($times[2][$key]=="")){
                        $Templatesource=str_replace("$value",time(),"$Templatesource");
                    }else{
                        $Templatesource=str_replace("$value",date($times[2][$key],time()),"$Templatesource");
                    }
                }else{

                    $Templatesource=str_replace("$value",date($times[2][$key],$times[1][$key]),"$Templatesource");
                }
            }
        }
        //not working now
        /*preg_match_all('|{function ([a-zA-Z]+)\((.*)\)}|Usi',$Templatesource,$functions);echo_r($functions);
        if(($functions[1][0]!==" ")and(preg_match("|{(.*)}|Us",$Templatesource)==0)){
        foreach ($functions[0] as $key=>$value){
        //$functions[2][$key] function name;
        //$functions[2][$key] data to send;
        $functionvalue=$functions[1][$key]($functions[2][$key]);
        $Templatesource=str_replace($value,$functionvalue,$Templatesource);
        }
        }
        */
        return $Templatesource;
    }

    function IncludePlugin(){

    }

    function CheckFile($file){
        if(file_exists($this->TemPath.$file)){
            return true;
        }else{
            $this->Error("tem_not_exit",$this->TemPath.$file,true);
        }
    }

    function ReadFile($file){
        $content=file_get_contents($this->TemPath.$file);
        if($content!==null){
            return $content;
        }else{
            $this->Error("tem_empty",$this->TemPath.$file,false);
        }
    }


    function assignArray($array,$arrayvalue){
        if(is_array($arrayvalue)){
            $this->elementvaluearray["array"][$array]=$arrayvalue;
        }
    }

    function assign($constans,$value){
        $this->elementvaluearray["constans"][$constans]=$value;
    }

    function Error($code,$error,$killer){
        $codearray=array(
        tem_not_exit=>"Template $error Not exit",
        tem_empty=>"Template $error is Empty",
        foreach_error=>"Invalid input for foreach , Foreach needs an array and $error isn't an array "
        );
        if($this->debug==true){
            if($killer==true){
                echo $codearray[$code];
                exit;
            }else{
                echo $codearray[$code];
            }
        }
    }
}
Return current item: Smart Engine Template Engine