Location: PHPKode > scripts > Simple Template class > simple-template-class/class.template.php
<?php

/**
 * Template
 *
 * @package Template
 * @author Vlad Litovka mailto:hide@address.com
 * @License: GNU Public License
 * @version 1.0
 * @access public
 */
class Template {
    var $PathOfTemplates;
    var $_data = array();

    /**
     * Template::Template()
     *
     * @param string $path
     */
    function Template($path = '')
    {
        if (!empty($path) && is_dir($path)) {
            $this->PathOfTemplates = $path;
        } else {
            $this->PathOfTemplates = '';
        }
        return true;
    }

    /**
     * Template::CompileData()
     *
     * @return
     */
    function CompileData()
    {
        foreach ($this->_data as $key => $val) {
            $this->$key = $val;
        }
        return true;
    }

    /**
     * Template::assign()
     *
     * @param mixed $key
     * @param mixed $value
     * @return
     */
    function assign($key, &$value)
    {
        if (isset($this->_data[$key])) {
            return false;
        }
        $this->_data[$key] = $value;
        return true;
    }

    /**
     * Template::display()
     *
     * @param mixed $file
     * @return
     */
    function display($file)
    {
        $this->template = $file;
        if (is_file($this->PathOfTemplates . $this->template)) {
            $this->CompileData();
            $this->RequireTemplate();
        } else {
            die("Can't load template file: " . $this->PathOfTemplates . $this->template);
            return false;
        }
        return true;
    }

    /**
     * Template::RequireTemplate()
     *
     * @return
     */
    function RequireTemplate()
    {
        return include $this->PathOfTemplates . $this->template;
    }
}

?>
Return current item: Simple Template class