Page 1 of 2

Creating a template system

Posted: Fri Aug 23, 2002 2:51 pm
by Takuma
I'm trying to create a template system but haven't got a good idea. What I though was I can open the template file and put it into an array and use ereg_replace or something to replace the text. Do you think there is (or are) better way of doing it?

Posted: Fri Aug 23, 2002 2:57 pm
by hob_goblin
http://www.devnetwork.net/forums/viewtopic.php?t=2513

also, what does the OS have to do with it?

Posted: Fri Aug 23, 2002 3:49 pm
by Takuma
OS is nothing to do with this... I was just wondering anyway thanks for your help! But not what I was thinking of...

Posted: Fri Aug 23, 2002 3:54 pm
by nielsene
If we use both should we vote twice?

Posted: Fri Aug 23, 2002 4:14 pm
by m3mn0n
i don't think you can... :?

Posted: Sat Aug 24, 2002 4:17 am
by conthox
to Nielsen

vote at the OS that you primary use

I've created it but..

Posted: Sat Aug 24, 2002 2:26 pm
by Takuma
Now I've made the following code for the template thing. Do you think this would work...?

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;

    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;
    &#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;
?>
Thanks.

Posted: Sat Aug 24, 2002 3:56 pm
by Takuma
Any luck poeple? Please help!

Posted: Sat Aug 24, 2002 4:21 pm
by hob_goblin
Why don't you try it and tell us if it works?

Posted: Sun Aug 25, 2002 12:25 am
by Takuma
OK...

After trying

Posted: Sun Aug 25, 2002 1:59 am
by Takuma
Here's what I got till but I have a problem...

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;      //File Handle for the template file
    var $key;            //Stores the variable for tempalte
    var $num;

    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($this->root_folder."/".$value,"r");
        $this->key&#1111;$i] = $key;
        $this->num++;
      &#125;
    &#125;

    function assign($template_vairable,$value) &#123;
      for($i = 0; $i != $this->num; $i++) &#123;echo $this->filenames&#1111;$i];;
        $this->file_content&#1111;$i] = fread($this->vairables&#1111;$i],filesize($this->filenames&#1111;$i]));
        $this->file_content&#1111;$i] = str_replace("&#123;$template_vairable&#125;",$value,$this->file_content);
      &#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;
    &#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;
?>
in the function "assign" it tries to access $this->filename and $this->variable but it seems that it's empty. I tried accessing it on function "define" and it holds a value. This is causing the following error when I execute with the following code. The code above is called template.php and under a folder called "library"

Code: Select all

<?php
  include("library/template.php");

  $tpl = new class_template();

  $tpl->define("library",array("basic" => "tpl.tpl"));

  $tpl->assign("CONTENT","hey");
?>
When I execute this I get this error:-
PHP Parser wrote:Warning: stat failed for (errno=2 - No such file or directory) in /usr/home/ticktaku/public_html/library/template.php on line 43

Warning: fread(): supplied argument is not a valid File-Handle resource in /usr/home/ticktaku/public_html/library/template.php on line 43
Now I have not got any idea why $this->filenames and $this->variables becomes empty... Please help :!:

Posted: Sun Aug 25, 2002 2:26 am
by hob_goblin

Code: Select all

$this->filenames&#1111;$i] = $value; 
        $this->variables&#1111;$i] = fopen($this->root_folder."/".$value,"r"); 
        $this->key&#1111;$i] = $key;

there is no $i variable

Few one final step

Posted: Sun Aug 25, 2002 2:55 am
by Takuma
After thinking for a while I've come up with this code (hot_goblin thanks alot 8) )

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;      //File Handle for the template file
    var $key;            //Stores the variable for tempalte
    var $num;

    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;$this->num] = $value;
        $this->variables&#1111;$this->num] = fopen($this->root_folder."/".$value,"r");
        $this->key&#1111;$this->num] = $key;
        $this->num++;
      &#125;
    &#125;

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

    function print_template($order) &#123;
      $order = explode(",",$order);
      $order_num = count($order);
      $order_num = $order_num - 1;

      for($i = 0; $i <= $order_num; $i++) &#123;
        for($n = 0; $n <= $i; $n++) &#123;
          if($this->key&#1111;$n] == $order&#1111;$i]) &#123;
            list($content) = $this->file_content&#1111;$i];
            echo $content;
          &#125;
        &#125;
      &#125;
    &#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;
?>
This does everything fine except that when replacing {$template_variable} to $value the { and } still remains, why is that?

Posted: Mon Aug 26, 2002 12:50 am
by Takuma
Any ideas?

Posted: Mon Aug 26, 2002 12:57 am
by hob_goblin
becuase { and } are a way to escape variables... try

"{".$var."}"