TempLight.php
<?php

    ####
    # TempLight v0.2
    # Michel Ypma
    # Netherlands 2008
    # www.michelypma.com
    ##

    class		TempLight
    implements 	ArrayAccess, Iterator, Countable
 	{

//__________________________________________________________________________________________________
//																		         A T T R I B U T E S

        private $__globals;                     // hold assigned variables
        private $__templateFilename;            // target template preset
        private $__templateDir;                 // target template directory
        
        private $__viewOnDestruct       = false;// view template on destruct
        private $__extractVars          = true; // extract the __globals
        private $__outputCompression    = false;// compress the output

//__________________________________________________________________________________________________
//																		               S T R U C T S

        private function __construct		( )
        {
            $this->__globals = array();
        }

        public function __destruct		( )
        {
            if ( $this->__viewOnDestruct )
                try {
                    $this->__viewOnDestruct = false;
                    $this->view();
                } catch ( Exception $e )
                {
                    print $e->__toString();
                }
        }

//__________________________________________________________________________________________________
//																 G E T T E R S  A N D  S E T T E R S

        public function __get   ( $_key )
        {
            return $this->offsetGet ( $_key );
        }

        public function __set   ( $_key, $_value )
        {
            $this->offsetSet ( $key, $value );
        }

//__________________________________________________________________________________________________
//																				   S I N G L E T O N
        /**
         * return instance of TempLight
         *
         * @return TempLight
         */
        public static function getInstance()
        {
            static $instance;

            if (! is_object ( $instance ) )
            {
                $obj = __CLASS__;
                $instance = new $obj();
            }
            return $instance;
        }

//__________________________________________________________________________________________________
//																				   S H O R T C U T S

        public function add ( $_key = '', $_global )
        {
            if ( strlen ( $_key ) > 0 )
            {
                if ( ! $this->offsetExists ( $_key ) )
                {
                    $this->__globals [ $_key ] = $_global;
                } else
                {
                    throw new Exception( 'Duplicate entry found on ' . $_key );
                }
            } else
            {
                $this->__globals[] = $_global;
            }
        }

        public function delete ( $_key )
        {
            $this->offsetUnset ( $_key );
        }

        public function clear ()
        {
            $this->__globals = array();
        }
        
        public function get ( $_offset )
        {
            return $this->offsetGet ( $_offset );
        }

//__________________________________________________________________________________________________
//																	implement: A R R A Y A C C E S S

        public function offsetExists ( $_offset )
        {
            return isset ( $this->__globals [ $_offset ] );
        }

        public function offsetGet ( $_offset )
        {
            if ( $this->offsetExists ( $_offset ) )
                return $this->__globals [ $_offset ];
            else
                throw new RangeException( 'Unknown var ' . $_offset . 'used' );
        }

        public function offsetSet ( $_offset, $_value )
        {
            if ( $this->offsetExists ( $_offset ) )
                $this->__globals [ $_offset ] = $_value;
            else
                throw new RangeException( 'Unknown var ' . $_offset . 'used' );
        }

        public function offsetUnset ( $offset )
        {
            if ( $this->offsetExists ( $offset ) )
                unset ( $this->__globals [ $offset ] );
            else
                throw new RangeException( 'Index out of bounds' );
        }

//__________________________________________________________________________________________________
//																		  implement: I T E R A T O R

        public function current	()
        {
            return current ( $this->__globals );
        }

        public function next	()
        {
            return next ( $this->__globals );
        }

        public function key		()
        {
            return key ( $this->__globals );
        }

        public function rewind	()
        {
            reset ( $this->__globals );
        }

        public function valid	()
        {
            return $this->current() !== false;
        }

//__________________________________________________________________________________________________
//																		implement: C O U N T A B L E

        public function count 	()
        {
            return count ( $this->__globals );
        }

//__________________________________________________________________________________________________
//																				   	 T E M P L A T E

        /**
         * Path to your templates directory
         *
         * @param string $_path
         */
		public function setTemplateDirectory( $_path )
		{
			$this->__templateDir     = $_path;
		}
		
		/**
		 * Enable output compression
		 *
		 * @param boolean $_bool
		 */
		public function enableOutputCompression( $_bool )
		{
			$this->__outputCompression     = $_bool;
		}

		/**
		 * Template filename
		 *
		 * @param string $_file
		 */
		public function setTemplate( $_file )
		{
			$this->__templateFilename    = $_file;
		}
		
		/**
		 * Automatic view template on ending
		 *
		 * @param boolean $_bool
		 */
		public function viewOnDestruct( $_bool )
		{
		    $this->__viewOnDestruct = (bool) $_bool;
		}
		
		/**
		 * acces values by straight variables
		 *
		 * @param boolean $_bool
		 */
		public function extractVars( $_bool )
		{
		    $this->__extractVars = (bool) $_bool;
		}

		/**
		 * send the template to the browser
		 *
		 * @param string $_filename
		 * @return boolean
		 */
		public function view( $_filename = '' )
		{
		    if ( $_filename != '')
                $this->__templateFilename = $filename;
                
		    if ( $this->__viewOnDestruct == true )
                throw new Exception( 'Calling TempLight::view() when viewOnDestruct is enabled isn\'t allowed' );
		    
            $output = $this->getTemplateData();
            
		    if ( $this->__outputCompression == true )
                $output = $this->compressOutput( $output );
            print $output;
		}

		/**
		 * returns the evaluated template
		 *
		 * @param string $_filename
		 * @return string
		 */
		public function result( $_filename = '' )
		{
		    if ( $_filename != '')
                $this->__templateFilename = $filename;
            
		    return $this->getTemplateData();
		}
		
		private function getTemplateData()
		{
		    ob_start ( );
		    
		    if ( $this->__extractVars )
		        extract( $this->__globals );

            if ( !$this->__templateFilename )
                throw new Exception('Missing template filename');
                
            include( $this->__templateDir . $this->__templateFilename . '.php' );
            
            return ob_get_clean ( );
		}
		
//__________________________________________________________________________________________________
//                                                              A D D I T I O N A L  F E A T U R E S

        public function compressOutput( $output )
        {
        	require_once('HTMLCompressor.php');
            $Compressor = new HTMLCompressor( $output, array('pre','script','textarea','code') );
            $Compressor->convert();
            return $Compressor->getOutput();
        }
	}

?>

HTMLCompression.php :
<?php

    ####
    # HTMLCompressor v0.1
    # Part of TempLight v0.2
    # Michel Ypma
    # Netherlands 2008
    # www.michelypma.com
    ##
	
	class HTMLCompressor {
	    
//__________________________________________________________________________________________________
//																		         A T T R I B U T E S
		
	    private $__output;
	    
	    private $__preserveData		= array();
	    private $__preserveBlocks	= array();
	    
//__________________________________________________________________________________________________
//																		               S T R U C T S
	    
	    public function __construct ( $_output, array $_preserveBlocks = array() )
	    {
	        $this->setOutput		( $_output );
	        $this->__preserveBlocks	= $_preserveBlocks;
	    }
	    
	    public function convert ()
	    {	        
	        $this->__output = $this->compressHorizontally       ( );
	        $this->__output = $this->compressVertically         ( );
	        $this->__output = $this->stripHTMLComments			( );
	        
	        return $this->__output;
	    }
	    
//__________________________________________________________________________________________________
//																 					   G E T T E R S
	    
	    public function getOutput()
	    {
	    	if ( $this->__preserveBlocks )
	                $this->restorePreserves ( $block );
	        return $this->__output;
	    }
	    
//__________________________________________________________________________________________________
//																 					   S E T T E R S
	    
	    public function setOutput( $_str )
	    {
	    	$this->backupPreserves();
	        $this->__output = $_str;
	    }
	    
//__________________________________________________________________________________________________
//																 			   C O M P R E S S I O N
	    
	    public function compressHorizontally ()
	    {
	        $this->__output = preg_replace("/((?)\n)[\s]+/m", '\1', $this->__output);
	        return $this->__output;
	    }
	    
	    public function compressVertically ()
	    {
	        $this->__output = str_replace("\n",'', $this->__output);
	        return $this->__output;
	    }
	    
	    public function stripHTMLComments ()
	    {
	        $this->__output = preg_replace( '/<!--.*?-->/', '', $this->__output );
	        return $this->__output;
	    }
	    
//__________________________________________________________________________________________________
//																 			P R E S E R V E  D A T A
	    
	    public function preserveBlock	( $elm )
	    {
	        preg_match_all('#<' . $elm . '.*>(.*)<\/' . $elm . '>#ismU', $this->__output, $matches, PREG_SET_ORDER);
	        $this->__output = preg_replace('#<' . $elm . '.*>(.*)<\/' . $elm . '>#ismU', '@@@'.$elm.'@@@', $this->__output );
	        $this->__preserveData[ $elm ]     = $matches;
	    }
	    
	    public function backupPreserves	()
	    {
            foreach( $this->__preserveBlocks as $block )
                $this->preserveBlock( $block );
	    }
	    
	    public function restorePreserves ()
	    {
	        foreach( $this->__preserveData as $elm => $datas )
	            foreach( $datas as $k => $v)
	                $this->__output = preg_replace( '#@@@'.$elm.'@@@#', $v[0], $this->__output, 1 );
	    }
	    
	}
?>
in je config:
<?php
// require TempLight
require_once ( 'TempLight.php' );

// get TempLight instance
$TempLight = TempLight::getInstance ();

// templates directory
$TempLight->setTemplateDirectory ( 'templates/' );

// extract vars?
$TempLight->extractVars ( true );

// ondestruct
$TempLight->viewOnDestruct ( true );

// output compression
$TempLight->enableOutputCompression ( true );
?>

programmeer logica:
<?php
// you can define the template here, it will be shown automatic on destruct
$TempLight->setTemplate ( 'index.tpl' );

/**
 * some nifty code logic here
 */

//assign a variable its value for template usage
$TempLight->add ('foo','bar');

// this won't affect your template vars... yes you're free to go in your codelogic
$foo = 'something else';

//in case you don't want to show the template on destruct you can call the view method
//$TempLight->view ();

// result will return the evaluated template
//echo $TempLight->result ();
?>

template:
[code]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <title>TempLight Example</title>
</head>

<body>
<p>
$foo; geeft: <?php echo $foo; ?> als ondestruct is geconfigureerd<br />
$this['foo']; geeft: <?php echo $this['foo']; ?><br />
$this->foo; geeft: <?php echo $this->foo; ?>
</p>
</body>

</html>
[/code]