SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Not Bad, eh? Justin Sampson's Avatar
    Join Date
    Aug 2000
    Location
    N.S., Canada
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Template Class, I'm confused and really need help :(

    Hello all,

    I'm sure there must be someone here that can help me with this. When I run template.php I get this error:

    Warning: The length of the needle must not be 0 in template.class on line 46

    template.php:
    PHP Code:
    <?
    include("template.class");

    $page_title = "title";
    $bg_color = "white";
    $user_name = "Justin";

    $template = new template;

    $template->register_file("home", "test.template");
    $template->register_variables("home", "page_title,bg_color,user_name");
    $template->file_parser("home");
    $template->print_file("home")
    ?>
    template.class:
    PHP Code:
    <?
    class template {
        VAR $files = array();
        VAR $variables = array();
        VAR $oppinging = '{!';
        VAR $closing = '!}';

    //register file
    function register_file($file_id, $file_name) {

    $fh = fopen($file_name, "r") or die ("Couldn't open $file_name!");
    $file_contents = fread($fh, filesize($file_name));

    $this->files[$file_id] = $file_contents;
    fclose($fh);
    }
    //end register file

    //register variables
    function register_variables($file_id, $variable_name) {

    $input_variables = explode(",", $variable_name);

    while (list(,$value) = each($input_variables)) {
    $this->variables[$file_id][] = $value;
    }

    }
    //end register variables

    //file parser
    function file_parser($file_id) {
    $varcount = count($this->variables[$file_id]);
    $keys = array_keys($this->files);

    if ((in_array($file_id, $keys)) && ($varcount > 0)) {
    $x = 0;
    while ($x < sizeof($this->variables[$file_id])) {
    $string = $this->varibales[$file_id][$x];

    GLOBAL $$string;

    $needle = $this->opening_escape.$string.$this->closing_escape;
    $this->files[$file_id] = str_replace(
    $needle,
    $$string,
    $this->files[$file_id]);
    $x++;
    }
    }

    }
    //end file parser

    function print_file($file_id) {
    print $this->files[$file_id];
    }
    }
    ?>
    I know it's kind of complicated but I really need to get this working!

    Thanks for the help,
    Justin Sampson
    Last edited by Justin Sampson; Oct 1, 2001 at 19:14.

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There wer ea few typos and messed up variable names in there.

    PHP Code:
    <?
    class template {
        VAR $files = array();
        VAR $variables = array();
        VAR $oppinging = '{!';
        VAR $closing = '!}';

    //register file
    function register_file($file_id, $file_name) {
        $fh = fopen($file_name, "r") or die ("Couldn't open $file_name!");
        $file_contents = fread($fh, filesize($file_name));
        
        $this->files[$file_id] = $file_contents;
        fclose($fh);
        }
    //end register file

    //register variables
    function register_variables($file_id, $variable_name) {
        $input_variables = explode(",", $variable_name);
        while (list(,$value) = each($input_variables)) {
            $this->variables[$file_id][] = $value;
            }
        }
    //end register variables

    //file parser
    function file_parser($file_id) {
        $varcount = count($this->variables[$file_id]);
        $keys = array_keys($this->files);
        
        if ((in_array($file_id, $keys)) && ($varcount > 0)) {
            $x = 0;
            while ($x < sizeof($this->variables[$file_id])) {
                $string = $this->variables[$file_id][$x];
                
                GLOBAL $$string;
                $needle = $this->oppinging.$string.$this->closing;
                $this->files[$file_id] = str_replace($needle,$$string,$this->files[$file_id]);
                $x++;
                }
            }
        
        }
    //end file parser

    function print_file($file_id) {
        print $this->files[$file_id];
        }
    }




    ?>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    Not Bad, eh? Justin Sampson's Avatar
    Join Date
    Aug 2000
    Location
    N.S., Canada
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks yet again Freddy, I guess I needed some sleep.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •