Return to Snippet

Revision: 16725
at August 13, 2009 11:38 by iTony


Initial Code
<?php
class Template{
    private $dir;
    private $data = array();

    public function __construct($dir) {
        if( is_dir($dir) ) {
            $this->dir = $dir;
        } else {
            throw PHPTemplateException('Directory does not exists');
        }
    }

    public function assign($variable, $data) {
        $this->data[$variable] = $data;
    }

    public function display($template) {
        $template = rtrim($template, '.php') . '.php';
        if( file_exists($this->dir . $template) ) {
            ob_start();

            $this->__fetch($this->dir . $template);

            ob_end_flush();
            return;
        }

        throw PHPTemplateException('File does not exists');
    }

    private function __fetch($____FILE) {
        extract( $this->data );
        include( $____FILE );
    }
}

class PHPTemplateException extends Exception {
    public function __construct($message) {
        parent::__construct('[PHP Template]:' . $message);
    }
}

Initial URL
http://www.talkphp.com/general/4844-php-template.html

Initial Description

                                

Initial Title
Basic PHP Templates Class

Initial Tags
class, php, template

Initial Language
PHP