Jump to content

Wer Lust hat kann sich mal mein Template System angucken...


Recommended Posts

... Als Vorlage habe ich das Tutorial auf dieser Seite von electr0n benutzt. Also sind einige Sachen ziemlich ähnlich. Denoch habe ich ein paar Sachen geändert und wenn jemand ein bischen Zeit hat, kann er sich dieses ja mal angucken und mir Feedback geben, ob das alles so Sinnvoll ist oder ob es bessere möglichkeiten gibt etwas zu machen. Vielleicht habe ich ja auch eine ganz wichtige Sache vergessen die in ein gutes Template System gehört.


Die Klasse ansich sieht so aus:

display_errors = $display_errors;
           }
           else {
               $this->error($function_name,'The Parameter \$display_errors is no boolean.');
           }

       }

       if($template_dir != '') {

           if(is_dir($template_dir)) {
               $this->template_dir = $template_dir;
           }
           else {
               $this->error($function_name,'The Parameter \$template_dir is no directory.');
           }

       }
       return true;
   }

   /**
    * Load the template file.
    *
    * @access public
    * @return boolean
    *
    * @param string template_file
    */
   function load_template($template_file='')
   {
       $function_name = 'load_template';

       if(!$template_file) {
           $template_file = $this->get_template_file_name();
       }

       if(!empty($this->template_cache)) {
           $this->error($function_name,'The template cache is not empty.');
       }

       $this->template_cache = $this->load_file($template_file);
       return true;
   }

  /**
    * Parses the template replacements.
    *
    * @access public
    * @return boolean
    *
    * @param array $array
    */
   function parse_reps($array)
   {
       $this->set_reps_array($array);
       $cache = $this->compile_reps($this->template_cache);
       $this->template_cache=$cache;
       return true;
   }

   /**
    * Parses the template includes.
    *
    * @access public
    * @return booelan
    *
    * @param string $number
    */
   function parse_includes($number='detect',$cache='')
   {
       $number = $this->get_number($number,'INCLUDE',$cache);

       for($i=0;$iget_parse_infos('INCLUDE',$cache);
         $include_cache = $this->load_file($parse_infos['name']);
         $include_cache = $this->compile_reps($include_cache);


         $this->template_cache = str_replace($parse_infos['code'],$include_cache,$this->template_cache);

         if($number2 = substr_count($include_cache,$this->left_delimiter.'INCLUDE "')) {
             $cache2 = $this->parse_includes($number2,$include_cache);
         }
       }
       return true;
   }

   /**
    * Parses the template includes.
    *
    * @access public
    * @return boolean
    *
    * @param string $number
    */
   function parse_loops($number='detect')
   {
       $function_name = 'parse_loops';
       $number = $this->get_number($number,'LOOP');

       for($i=0;$iget_parse_infos('LOOP');

           if(!$this->loop_array[$parse_infos['name']]) {
               $this->error($function_name,'No array is set for the loop "'.$parse_infos['name'].'"');
           }

           foreach($this->loop_array[$parse_infos['name']] as $name=>$value) {
               $cache.= $this->compile_reps($parse_infos['code'],$value);
           }

           $this->template_cache=str_replace($parse_infos['code_tag'],$cache,$this->template_cache);
       }
       return true;
   }

   /**
    * Parses the template includes.
    *
    * @access public
    * @return boolean
    *
    * @param string $number
    */
   function parse_ifs($number='detect')
   {
       $function_name = 'parse_ifs';
       $number = $this->get_number($number,'IF');

       for($i=0;$iget_parse_infos('IF');

           if(!$this->loop_array[$parse_infos['name']] or $this->loop_array[$parse_infos['name']] == 0) {
               $this->template_cache = str_replace($parse_infos['code_tag'],$parse_infos['else_code'],$this->template_cache);
           }
           else {
               $this->template_cache = str_replace($parse_infos['code_tag'],$parse_infos['if_code'],$this->template_cache);
           }



       }
       return true;
   }

   /**
    * Set the rep_array.
    *
    * @access public
    * @return boolean
    *
    * @param array $array
    */
   function set_reps_array($array='')
   {
       if(is_array($array) and $array != '') {
           foreach($array as $name=>$value){
                eval('$this->reps_array['.$name.'] = "'.$value.'";');
           }
       }
       return true;
   }

   /**
    * Set the loop_array.
    *
    * @access public
    * @return bloolean
    *
    * @param string $name
    * @param array $array
    */
   function set_loop_array($array_name,$array)
   {
       $function_name = 'set_loop_array';
       if(is_array($array)) {
            $this->loop_array[$array_name] = $array;
       }
       else {
            $this->error($function_name,'No Array was set for the loop "'.$array_name.'"');
       }
       return true;
   }

   /**
    * Set the if_array.
    *
    * @access public
    * @return bloolean
    *
    * @param string $name
    * @param array $array
    */
   function set_if_array($array_name,$array='')
   {
       $function_name = 'set_if_array';
       if(is_integer($array) or $array == '') {
            $this->loop_array[$array_name] = $array;
       }
       else {
            $this->error($function_name,'No Integer was set for the if "'.$array_name.'"');
       }
       return true;
   }

   /**
    * Prints the template.
    *
    * @access public
    * @return boolean
    */
   function output()
   {
       $function_name = 'output';

       if(empty($this->template_cache)) {
           $this->error($function_name,'The template cache is empty.');
       }
       echo $this->template_cache;
       return true;
   }

   /**
    * Returns the template.
    *
    * @access public
    * @return string
    */
   function fetch()
   {
       $function_name = 'fetch';

       if(empty($this->template_cache)) {
           $this->error($function_name,'The template cache is empty.');
       }
       return $this->template_cache;
   }

   /**#@+
    * class functions @private
    */

   /**
    * Display the Errors.
    *
    * @access private
    * @return void
    *
    * @param string $function_name,$errormsg
    */
   function error($function_name,$errormsg)
   {
       $errormsg ='Error in '.$this->classname.': '.$errormsg.'';
       $errormsg.='Function: '.$function_name.'';
       $errormsg.='File: '.__FILE__.'';
       $errormsg.='Script: '.getenv('REQUEST_URI').'';
       $errormsg.='Date: '.date('d.m.Y @ H:i').'';

       if($this->display_errors == FALSE) {
           $errormsg = '';
       }
       else {
           $errormsg = ''.$errormsg.'';
       }

       die($errormsg);
   }
   /**
    * Get the request_uri.
    *
    * @access private
    * @return string
    */
   function get_request_uri()
   {
       $request_uri = $_SERVER['REQUEST_URI'];

       if(!$request_uri) {

           if($_SERVER['PATH_INFO']) {
               $request_uri = $_SERVER['PATH_INFO'];
           }
           else {
               $request_uri = $_SERVER['PHP_SELF'];
           }

           if($_SERVER['QUERY_STRING']) {
               $request_uri.="?".$_SERVER['QUERY_STRING'];
           }
       }

       $request_uri = substr(basename($request_uri), 0, 250);

       if(!strstr($request_uri,".")) {
           $request_uri="index.php";
       }

       return $request_uri;
   }

   /**
    * Get the template file name.
    *
    * @access private
    * @return string
    */
   function get_template_file_name()
   {
       $request_uri = $this->get_request_uri();
       return substr($request_uri,'0',strpos($request_uri,'.php'));
   }

   /**
    * Get the number of the typs in the template
    *
    * @access private
    * @param string $number, $typ
    * @return string
    */
   function get_number($number,$typ,$cache='')
   {
       $function_name = 'get_number';
       if(!$cache) {
           $cache = $this->template_cache;
       }

       switch($number) {
           case 'detect':
               $number = substr_count($cache,$left_delimiter.$typ.' "');
               return $number;
               break;

           case 'none':
               return 0;
               break;

           default:
               if(is_integer($number)) {
                   return $number;
               }
               else {
                   $this->error($function_name,'The number of '.$typ.'S is false');
               }
       }
   }
   /**
    * Get the parse informations for LOOPS, INCLUDES and IFS
    *
    * @access private
    * @param string $typ
    * @return array
    */
   function get_parse_infos($typ,$cache='')
   {
       $function_name  = 'get_parse_info';

       if(!$cache) {
           $cache = $this->template_cache;
       }

       $start_pos_code = strpos($cache,$this->left_delimiter.$typ.' "');

       if(!$start_pos_code) {
           $this->error($function_name,'No '.$typ.' was found');
       }

       $start_pos_name = $start_pos_code+strlen($typ)+3;
       $end_pos_name = strpos($cache,'"',$start_pos_name);
       $name = substr($cache,$start_pos_name,$end_pos_name-$start_pos_name);

       if($typ == 'INCLUDE') {
           $end_pos_code = strpos($cache,'"'.$this->right_delimiter,$start_pos_code);
           $code = substr($cache,$start_pos_code,$end_pos_code-$start_pos_code+2);
       }

       if($typ == 'LOOP' or $typ == 'IF') {
           $loop_end_tag   = $this->left_delimiter.'/'.$typ.$this->right_delimiter;
           $loop_start_tag = $this->left_delimiter.$typ.' "';
           $loop_start_tag2= '"'.$this->right_delimiter;

           do {

               if($end_pos_code) {
                   $pos = $end_pos_code+strlen($loop_end_tag)+3;

               }
               else {
                   $pos = $start_pos_code;
               }

               $end_pos_code = strpos($cache,$loop_end_tag,$pos);
               $strlen_start_tag = strlen($loop_start_tag.$name.$loop_start_tag2);
               $code = substr($cache,$start_pos_code+$strlen_start_tag,$end_pos_code-($start_pos_code+$strlen_start_tag));
               $code_tag = substr($cache,$start_pos_code,($end_pos_code-$start_pos_code)+strlen($loop_end_tag));
           }while(substr_count($code,$loop_start_tag) != substr_count($code,$loop_end_tag));
       }
       if($typ == 'IF') {
       $else_tag = $this->left_delimiter.'ELSE "'.$name.'"'.$this->right_delimiter;

       $start_pos_else = strpos($code,$else_tag);
       $end_pos_else = $start_pos_else+strlen($else_tag);

       $if_code = substr($code,0,$start_pos_else);
       $else_code = substr($code,$end_pos_else);
       }
       return array('name'=>$name,'code'=>$code,'code_tag'=>$code_tag,'if_code'=>$if_code,'else_code'=>$else_code);
   }

   /**
    * Replacements
    *
    * @access private
    * @param string $cache
    * @return string
    */
   function compile_reps($cache,$array='')
   {
       $function_name = 'compile_reps';
       if(!$array) {
           $array = $this->reps_array;
       }

       if (count($array) error($function_name,'Missing variables.');
       }
       foreach($array as $name => $value) {
           $cache = str_replace($this->left_delimiter.'$'.$name.$this->right_delimiter,$value,$cache);
       }
       return $cache;
   }

   /**
    * Load the template file.
    *
    * @access private
    * @param string $cache
    * @return string
    */
   function load_file($template_file)
   {
       if(!is_file($this->template_dir.'/'.$template_file.'.'.$this->template_typ)) {
           $this->error($function_name,'The template file \''.$template_file.'.'.$this->template_typ.'\'  doesn\'t exist.');
       }

       if(filesize($this->template_dir.'/'.$template_file.'.'.$this->template_typ) == 0) {
           $this->error($function_name,'The template \''.$template_file.'.'.$this->template_typ.'\' file is empty.');
       }

       if(!$cache = implode('',file($this->template_dir.'/'.$template_file.'.'.$this->template_typ))) {
           $this->error($function_name,'Can not read from the template file '.$template_file.'.');
       }
       else {
           return $cache;
       }
   }
 }
?>



Ein TPL Datei sähe so aus:


 

hier wird was eingesetzt {$replace}.


{INCLUDE "include"}



{LOOP "liste"}
hier ein loop mit einer Variable {$variable}
{/LOOP}



{IF "access"}
Du darfst den Benutzerbereich betreten.
{ELSE "access"}
Du hast nicht die nötigen Rechte, um den Benutzerbereich zu betreten.
{/IF}



und die index.php sieht so aus

 

load_template('index');

//Replacemnets parsen
$template->parse_reps(array('replace'=>'replace'));

//Includes Parsen
$template->parse_includes();


$liste[] = array('variable'=>'asdf');
$liste[] = array('variable'=>'omg');

//Loop array setzten
$template->set_loop_array('liste',$liste);

//Loops parsen
$template->parse_loops();

//If array setzten 1=if 0=else
$template->set_if_array('access',1);

//IF parsen
$template->parse_ifs();

//ausgeben
$template->output();
?>



Für jegliche Kritik und anregung bin ich sehr dankbar.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...