Location: PHPKode > projects > crVCL PHP Framework > template.lib.php
<?PHP

/*

The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/MPL-1.1.html or see MPL-1.1.txt in directory "license"

Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for
the specific language governing rights and limitations under the License.

The Initial Developers of the Original Code are: 
Copyright (c) 2003-2012, CR-Solutions (http://www.cr-solutions.net), Ricardo Cescon
All Rights Reserved.

Contributor(s): Ricardo Cescon

crVCL PHP Framework Version 2.4
*/







############################################################
if(!defined("TEMPLATE_LIB")){
   define ("TEMPLATE_LIB", 1);
############################################################
/**
 * support if conditions in tamplate
 * 
 * Sample: <FWT_IF condition = "#var1# == 15">
 *             #text2#
 *         </FWT_IF>
 *
 *
 */
class template{
   	
   private $m_tmp_vars = null;
    	
   public $m_errormsg = "";	
   
//-------------------------------------------------------------------------------------------------------------------------------------         
   /**
    * parse a file 
    * 
    * @param string $file
    * @param array $vars Sample: array("text1"=>"Hello","text2"=>"World");
    * @param string $area => '' or 'head' or 'body'
    * @return string 
    */
   function parseFile($file, $vars, $area=""){
      $this->m_errormsg = "";
      
      if(is_file($file)==false){
        $this->m_errormsg = "template::parseFile - can't read template, template not found";
        return "";
      }
      
      $handle = fopen ($file, "r");
      if(!$handle){
         $this->m_errormsg = "template::parseFile - error read template";
         return "";
      }
      $stream = fread($handle, filesize($file));
      fclose($handle);
      
      return $this->parseStream($stream, $vars, $area);
   }   
//-------------------------------------------------------------------------------------------------------------------------------------
   /**
    * callback
    * 
    * @param array $args from pattern
    */
   private function parseStream_($args){
   	$key = &$args[0];
   	return isset($this->m_tmp_vars[$key]) ? $this->m_tmp_vars[$key] : $key;
   }
//-------------------------------------------------------------------------------------------------------------------------------------            
   /**
    * parse a stream
    * 
    * @param string $stream
    * @param array $vars Sample: array("text1"=>"Hello","text2"=>"World");
    * @param string $area => '' or 'head' or 'body'
    * @return string 
    */
   function parseStream($stream, $vars, $area=""){      
      $this->m_errormsg = "";
      
      if(is_object($stream) && ($stream instanceof multiAjaxHtmlResponse) == true){
         $stream = $stream->partsToHtml(); 
      }
      
      $template = &$stream;
                         
      $sub = false;
      if(strtolower($area) == 'head'){         
         $sub = true;
      }else if(strtolower($area) == 'body'){         
         $sub = true;
      }
      
      if($area!==""){
         $spos = strpos($template, '<'.$area);         
         if($spos === false){$this->m_errormsg = "template::parseStream - parse error: can't find <".$area; return '';}

         $template = substr($template,$spos);
         $spos = strpos($template, '>');
         if($spos === false){$this->m_errormsg = "template::parseStream - parse error: can't find ".$area.">"; return '';}

         $template = substr($template,$spos+1);
      
         $epos = stripos($template, '</'.$area.'>');
         if($epos === false){
             $epos = strpos($template, '</'.$area.'>');
         }
         if($epos === false){$this->m_errormsg = "template::parseStream - parse error: can't find </".$area; return '';}

         $template = substr($template,0,$epos-1);
      }
      
      $atmp = array();
      if(acount($vars) > 0){
         reset($vars);
         
         foreach($vars as $key => $val) {
         	
             if(is_object($val)){ 
                $obj = $val;                                
                if(is_subclass_of($obj, 'html_Component')){  #echo 'html_Component: ' .  $key . BR;               
                   if(is_a($obj, "html_Form") !== false){                                                                 
                                            
                      if(strpos($template, $key.".open") === false && strpos($template, $key.".close") === false){
                         $val = $obj->html();
                      }else{
                         $obj->html();  

                         if(strpos($template, $key.".childs") === false){
                            $atmp['#'.$key.'.open#'] = $obj->getOpentag().$obj->getChildtag();
                         }else{
                            $atmp['#'.$key.'.open#'] = $obj->getOpentag();
                            $atmp['#'.$key.'.childs#'] = $obj->getChildtag();
                         }
                         $atmp['#'.$key.'.close#'] = $obj->getClosetag();
                         $val = "";
                      }                     
                                                                                                                                  
                   }else{                   
                     $val = $obj->html();
                   }  
                }  
             }

             $val = strval($val);
             if($val != ""){ 
                $atmp['#'.$key.'#'] = $val;
             }               

         } // while
         $vars = &$atmp;
         
         $this->m_tmp_vars = &$vars;                           
         $template = preg_replace_callback('/#[^# <>\"\';\t\r\n]{3,100}#/', array($this, 'parseStream_'), $template);
         $this->m_tmp_vars = null;
         unset($this->m_tmp_vars);
      }         

      ####
      
      $fwt_if_count = 0;
      $temp = '';
      while( ($spos = strpos($template, '<FWT_IF')) !== false){
         $fwt_if_count++;
      
         $temp .= substr($template,0,$spos-1);
         $template = substr($template,$spos);
      
         $epos = strpos($template, '>');
           
         if($epos === false){
            $this->m_errormsg = "template::parseStream - '>' missing, error near ".$fwt_if_count." FWT_IF";
         }
      
         $condition = substr($template,7,$epos-6);
         $condition = trim(str_replace("condition", "", $condition));
         if(left($condition,1)!="="){
            $this->m_errormsg = "'template::parseStream - condition = ' missing, error near ".$fwt_if_count." FWT_IF";
         }
      
         $condition = trim(substr($condition,1));
         $condition = trim(str_replace(">", "", $condition));
      
         if(left($condition,1)=='"'){
            $condition = trim(str_replace("\"", "", $condition));
            $condition = trim(str_replace("'", "\"", $condition));
         }
         $condition = trim(str_replace("'", "", $condition));
         settype($condition,"string");
           
         $condition = 'return '.$condition.';';
      
      
         $eval = false;
         $olderr = error_reporting(0);
         $eval = eval($condition);
         error_reporting($olderr);
      
         $template = substr($template,$epos+1);
          
         $endtag = strpos($template, '</FWT_IF>');
         if($endtag === false){
            $this->m_errormsg = "</FWT_IF> missing, error near ".$fwt_if_count." FWT_IF";
         }
      
         if($eval){
            $temp .= substr($template,0,$endtag-1);
         }
         $template = substr($template,$endtag+9);
      }
      if($fwt_if_count>0){$template = $temp.$template;}
      
      return $template;
   }
}

############################################################
}
############################################################
?>
Return current item: crVCL PHP Framework