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:

Creating a template system

Post 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?
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

http://www.devnetwork.net/forums/viewtopic.php?t=2513

also, what does the OS have to do with it?
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post 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...
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

If we use both should we vote twice?
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

i don't think you can... :?
conthox
Forum Commoner
Posts: 39
Joined: Tue Jun 25, 2002 1:44 pm
Location: Sweden

Post by conthox »

to Nielsen

vote at the OS that you primary use
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

I've created it but..

Post 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.
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Any luck poeple? Please help!
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

Why don't you try it and tell us if it works?
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

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

After trying

Post 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 :!:
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Few one final step

Post 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?
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Any ideas?
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

becuase { and } are a way to escape variables... try

"{".$var."}"
Post Reply