Creating a template system

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Which OS do you use?

Windows
11
65%
Linux
6
35%
Others
0
No votes
 
Total votes: 17

User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

thanks man!
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

With help from everyone I've manage to make this:-

Code: Select all

<?php
  /*

    Template Function Library

  */

  class class_template &#123;
    var $root_folder;    //Stores root folder of template files
    var $file_content;   //Stores the content of tempalte files
    var $filenames;      //Stores the names of template files
    var $variables;      //Stores the variable name for the template
    var $num;            //Stores number of elements

    function define($folder,$template_variables) &#123;
      if(is_dir($folder)) &#123;
        $this->root_folder = $folder;
      &#125; else &#123;
        $this->error_report("The root directory specified is not a directory!");
        exit();
      &#125;
      $this->num = 0;
      while(list($key,$value) = each($template_variables)) &#123;
        if(!file_exists($this->root_folder.$value)) &#123;
          $this->error_report("$value does not exists!");
          exit();
        &#125;
        $this->filenames&#1111;$i] = $value;
        $this->variables&#1111;$i] = fopen($value,"r");
        $key&#1111;$i] = $key;
        $this->num++;
      &#125;
      $this->num = $this->num - 1;
    &#125;

    function assign($template_vairable,$value) &#123;
      for($i = 0; $i == $this->num; $i++) &#123;
        $this->file_content&#1111;$i] = fread($this->vairables&#1111;$i],filesize($this->filenames&#1111;$i]));
        if(strstr($this->variables&#1111;$i])) &#123;
          $this->file_content&#1111;$i] = str_replace("&#123;".$template_vairable."&#125;",$value,$this->file_content);
        &#125;
      &#125;
      unset($i);
    &#125;

    function print_print($order) &#123;
      $order = explode(",",$order);
      $order_num = count($order);
      $i = 0; $n = 0;
      while($i != $this->num) &#123;
        while($n != $this->num) &#123;
          if($order&#1111;$i] == $file_content&#1111;$n]) &#123;
            echo $file_content&#1111;$n];
            $n = 0;
            return false;
          &#125;
        &#125;
      &#125;
	  unset($i); unset($n);
    &#125;

    function error_report($error_msg) &#123;
      echo "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Unexpected error has occured</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<div align="center"> <span style="font-family: Verdana, Arial, Helvetica, sans-serif; color: #FF0000; font-size: 12px; font-weight: bold;">".$error_msg."</span>
</div>
</body>
</html>";
    &#125;
  &#125;
?>
Now this works fine but like FastTemplate, I want to make it so when I do

Code: Select all

assign(".CONTENT","Hello"); assign(".CONTENT","HIYA");
This prints CONTENT twice but my doesn't (obviously...). How do you think I can do it?
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Please Please Help... I have an idea now how about now, but the problem is how can I take "." off from a string ("." is at the start of the string).
Post Reply