Location: PHPKode > projects > PHP Object Oriented Framework > poof/Template.class.php
<?php
/*
    This file is part of POOF.

    POOF is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
    
    POOF is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with POOF; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

require_once(dirname(__FILE__) . "/Templates.class.php");
require_once(dirname(__FILE__) . "/Parser.class.php");

/**
* class Template
* The nested Template class. It links to the other Template objects that it is netsed with. It also has sub-templates as it's global and local variables.
* @package poof
* @author Brian Takita <hide@address.com>
* @version 0.4
*/
class Template {
    /**
    * @var string The name of the file the calling class is in.
    */
    var $file_name;
    /**
    * @var string The file name of the template file.
    */
    var $template_file_name;
    /**
    * @var string The content of the template.
    */
    var $content;
    /**
    * @var bool Does this class pass through parsing this template?
    */
    var $pass_through = true;
    /**
    * @var Template The current main Template Class.
    */
    var $current;
    /**
    * @var Template The parent main Template Class.
    */
    var $parent;
    /**
    * @var Template The child main Template Class.
    */
    var $child;
    /**
    * @var Template The root main Template Class.
    */
    var $root;
    /**
    * @var Template The youngest(no children) main Template Class.
    */
    var $leaf;
    /**
    * @var string The variable name in the parent template that $content will be inserted into.
    * @since Version 0.3.1
    */
    var $insert_var;
    /**
    * @var Templates The local variables for this main template.
    */
    var $vars;
    /**
    * @var Templates The global variables for the main templates.
    */
    var $global_vars;
    /**
    * @var    mixed A variable to put temporary data. This should not be used to store data.
    * @access private
    */   
    var $_temp;

    /**
    * The Template class constructor. Initializes the attributes in the Template class.
    * @var string $file_name The name of the file that initializes the Template object.
    */
    function Template($file_name = null) {
        $this->global_vars = &new Templates();
        $this->current = &$this;
        $this->root = &$this;
        $this->leaf = &$this;
        $this->vars = &new Templates();
        $this->file_name = $file_name;
    }

    /**
    * Creates and sets a parent Template object to the Current Template object.
    * @since  Version 0.4
    * @return Template The parent Template object.
    * @param  string $file_name  The name of the file where the parent Template is instantiated.
    * @param  string $insert_var The variable in the Parent Template that the Current Template will be inserted into.
    */
    function &set_parent($file_name, $insert_var) {
        $parent = &new Template($file_name);
        return $this->_set_parent($file_name, $insert_var, &$parent);
    }
    
    /**
    * Creates and sets a parent Object to the Current Object.
    * @since  Version 0.4
    * @return Template The parent Object.
    * @param  string   $file_name  The name of the file where the parent Object is instantiated.
    * @param  string   $insert_var The variable in the Parent Object that the Current Object will be inserted into.
    * @param  Template &$parent    The Parent Object that is inserted.
    */
    function &_set_parent($file_name, $insert_var, &$parent) {
        // Connect the global variables
        $parent->global_vars = &$this->global_vars;
        $parent->current = &$this->current;
        
        // If a parent already exists, then insert the parent between the current class and the parent.
        if (isset($this->parent)) {
            $parent->parent = &$this->parent;
            $this->parent->child = &$parent;
        } else {
            $this->root = &$parent;
        }

        $parent->root = &$this->root;
        $parent->leaf = &$this->leaf;

        $this->parent = &$parent;
        $parent->child = &$this;
        $this->insert_var = $insert_var;
        return $parent;
    }
    
    /**
    * Creates and sets a child Template object to the Current Template object.
    * @since  Version 0.4
    * @return Template The child Template object.
    * @param  string $file_name  The name of the file where the child Template is instantiated.
    * @param  string $insert_var The variable in the child Template that the Current Template will be inserted into.
    */
    function &set_child($file_name, $insert_var) {
        $child = &new Template($file_name);
        return $this->_set_child($file_name, $insert_var, &$child);
    }

    /**
    * Creates and sets a child Object to the Current Object.
    * @since  Version 0.4
    * @return Template The child Object.
    * @param  string   $file_name  The name of the file where the child Object is instantiated.
    * @param  string   $insert_var The variable in the Parent Object that the Current Object will be inserted into.
    * @param  Template &$child    The Parent Object that is inserted.
    */
    function &_set_child($file_name, $insert_var, &$child) {
        $child->global_vars = &$this->global_vars;
        $child->current = &$this->current;

        // If a child already exists, then insert the child between the current class and the child.
        if (isset($this->child)) {
            $child->child = &$this->child;
            $this->child->parent = &$child;
        } else {
            $this->leaf = &$child;
        }

        $child->root = &$this->root;
        $child->leaf = &$this->leaf;

        $child->parent = &$this;
        $this->child = &$child;
        $child->insert_var = $insert_var;
        return $child;
    }

    /**
    * The Template class Destructor.
    * @deprecated depreciated since Version 0.4
    * @since Version 0.3.1
    */
    function destroy() {
    }

    /**
    * Get the full file path of the file.
    * @return string The full file path.
    * @var    string The file.
    */
    function get_file($file) {
        if (substr(&$file, 0, 1) == "/") {
            return $file;
        } else {
            return $this->get_dir().'/'.$file;
        }
    }
    
    /**
    * Returns the current directory of the Template class.
    * @return string The Directory of the class.
    */
    function get_dir() {
        return dirname($this->file_name);
    }

    /**
    * Returns the content of the file if a file is passed to the method or string if a string is passed to the method.
    * @access private
    * @return The content of the file.
    * @param string    $content  The file name or string where the content will be extracted.
    * @param bool      $is_file  Is $content a file (true) or a string (false)?
    */
    function &_get_content($content, $is_file) {
        if ($is_file == true) {
            $content = $this->get_dir() . "/" . $content;

            if (!file_exists($content)) {
                print "File " . $content. " does not exist.";
                return "";;
            }
            $content = implode('', file($content));
        }
        return $content;
    }

    /**
    * Set the template file.
    *
    * @param string $template The Template file or text string.
    * @param bool   $is_file  Is the entered template a file? Default is true.
    */
    function set_template($template, $is_file = true) {
        if ($is_file) {
            $this->template_file_name = $this->get_file($template);
        } else {
            $this->template_file_name = $this->file_name;
        }
        $this->_set_template($template, $is_file, &$this);
    }

    /**
    * Set the template file in the specified Template Object.
    *
    * @param string   $template The Template file or text string.
    * @param bool     $is_file  Is the entered template a file? Default is true.
    * @param Template &$object  The Template Object that the template is being set.
    */
    function _set_template($template, $is_file, &$object) {
        $object->pass_through = false;
        $object->content = $object->_get_content($template, $is_file);
    }

    
    /**
    * Sets a sub-template in the Templates object &$templates.
    * @access private
    * @return Template The created variable.
    * @param  string            $name     The name of the variable.
    * @param  Template/string   &$content The content of the variable. If a Template object is passed, the Template object will be used. If a string is passed, then the string is used to create a new Template object.
    * @param  bool              $is_file  Is $content a file name?
    * @param  Templates         $templates The Templates object that the new variable Template object will be placed.
    */
    function &_set_var($name, &$content, $is_file, &$templates) {
        if (!is_string($content)) {
            return $templates->insert($name, &$content);
        } else {
            if ($is_file == true) {
                return $templates->create($name, $this->_get_content($content, $is_file), $content);
            } else {
                return $templates->create($name, $content, $this->file_name);
            }
        }
    }

    /**
    * Sets a local template variable.
    * @return  Template A reference to the template variable that was set.
    * @param   string   $name      The name of the local template variable.
    * @param   string   $conntent  The value of the local template variable.
    * @param   bool     $is_file   Is $val a file? Default is false.
    */
    function &set_var($name, $content, $is_file = false) {
        return $this->_set_var($name, &$content, &$is_file, &$this->vars);
    }

    /**
    * Set the global template variable.
    * @return Template The global variable template that was set.
    * @param string $name      The name of the local template variable.
    * @param string $content   The value of the local template variable.
    * @param bool   $is_file   Is $content a file? Default is false.
    */
    function &set_global_var($name, $content, $is_file = false) {
        return $this->_set_var(&$name, &$content, &$is_file, &$this->global_vars);
    }

    /**
    * Returns the current variables of the Template class.
    * @access private
    * @return array The variables to be parsed.
    */
    function &_get_all_vars() {
        return array_merge($this->global_vars->parse(), $this->vars->parse());
    }

    /**
    * Parse the templates and return the output.
    * @return string The Parsed data.
    */
    function parse() {
        $this->current = &$this->leaf;
        $this->current->global_vars->parse();

        while(1) {
            $this->current->vars->parse();

            if ($this->current->pass_through == true) {
                // If yes, then use what is in the content.
                $content = $this->current->content;
            } else {
                // When parsing the template file, change $this->file_name to the template's file name.
                $this->_temp = $this->current->file_name;
                $this->current->file_name = $this->current->template_file_name;
                // If no, parse the template.
                $content = Parser::parse($this->current->content, $this->current->_get_all_vars());
                $this->file_name = $this->_temp;
            }

            $parent = &$this->current->parent;

            if (!isset($parent)) {
                return $content;
            }

            if ($parent->pass_through == true) {
                $parent->content = &$content;
            } else {
                // If pass_through is not enabled in the next element, then pass the content into the insert variable.
                $parent->_set_var($this->current->insert_var, $content, false, $parent->vars);
            }

            $this->current = &$parent;
        }
        return $content;
    }

    /**
    * Parse the templates and print the output.
    * @return bool True if the parse was successful and False if unsuccessful.
    * @since  Version 0.3.1
    */
    function show() {
       return print($this->parse());
    }

    /**
    * Parse a local template variable and return the output.
    * @return string The result of the parsed template.
    * @param  string $var The template variable to be parsed.
    */
    function &parse_var($var) {
        return $this->vars->$var->parse();
    }

    /**
    * Parse a global template variable and return the output.
    * @return string The result of the parsed template.
    * @param  string $var The template variable to be parsed.
    */
     function &parse_global_var($var) {
        return $this->global_vars->$var->parse();
    }
}
?>
Return current item: PHP Object Oriented Framework