Location: PHPKode > scripts > Tmpl > tmpl/tmpl.class.php
<?php
/***************************************************************
 * vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker:
 *
 * PHP versions 4 and 5
 *
 * --LICENSE NOTICE--
 * This source file is subject to version 3.01 of the PHP license
 * that is available through the world-wide-web at the following URI:
 * http://www.php.net/license/3_01.txt.  If you did not receive a copy of
 * the PHP License and are unable to obtain it through the web, please
 * send a note to hide@address.com so we can mail you a copy immediately.
 * --LICENSE NOTICE--
 ***************************************************************
 * $File: tmpl.class.php $
 * $Author: mschulz $ - $Date: 2009/01/12 15:28:08 $
 * $Source: /var/lib/cvs/php-001/class/tmpl.class.php,v $ - $Revision: 1.0 $
 * $Description: Class to read and parse a template file. $
 * $Copyright: (c) 2009 Marko Schulz $
 ***************************************************************/


/**
 * {{{ @example
 *
 * <code>
 *
 * include_once( 'tmpl.class.php' );
 *
 * // Create Tmpl Object.
 * (object) $tpl = new Tmpl( array( 'file' => 'example.tpl' ) );
 *
 * // Parse and print out the template.
 * $tpl->replace( '[%TITLE%]', 'My Site' );
 * $tpl->replace( '[%BGCOLOR%]', '#f5f5f5' );
 * $tpl->replace( '[%COLOR%]', '#003366' );
 * $tpl->replace( '[%FONT%]', 'Verdana, Helvetica, Sans-Serif' );
 * echo $tpl->output();
 *
 * </code>
 *
 * }}}
 */


/**
 * The class provides methods to load and replace
 * placeholder of a defined template file.
 *
 * @category   Templates
 * @author     Marko Schulz
 * @copyright  2009
 * @license    http://www.php.net/license/3_01.txt  PHP License 3.01
 * @version    File: $Revision: 1.0 $
 */

class Tmpl {


	// {{{ properties
	/**
	 * template object
     *
	 * @var string
	 * @access public
	 */
	var $template;


	/**
	 * all error messages
     *
	 * @var array
	 * @static
	 * @access private
	 */
	private static $ERROR = array(
		'IOERROR' => "Can't open [%s] as template file!",
	);
    // }}}


	// {{{ constructor
	/**
	 * This is the Template Class Constructor.
	 *
	 * @access public
	 * @param array $args
	 * - string $args['file']
	 * @return object
	 */
	public function __construct( array $args = array() ) {

		if ( isset( $args['file'] ) &&  strlen($args['file']) > 0 )
            (object) $this->open( $args['file'] );

	}
    // }}}


	// {{{ open()
	/**
	 * This method open the template file
	 *
	 * @access private
	 * @param string &$file
	 * @return object
	 */
	private function open ( &$file ) {

		// Try to open the template file
		if ( !@file( $file ) ) $this->fatal( 'IOERROR', __LINE__, $file );
		else $this->template = implode( '', file( $file ) );

	}
    // }}}


	// {{{ replace()
	/**
	 * This method replace placeholder in the template file
	 *
	 * @access public
	 * @param string $match
	 * @param string $replace
	 * @return string
	 */
	public function replace ( $match, $replace ) {

		$this->template = str_replace( $match, $replace, $this->template );
		return $this->template;

	}
    // }}}


	// {{{ output()
	/**
	 * This method return the template file
	 *
	 * @access public
	 * @return string
	 */
	public function output () {

		return $this->template;

	}
    // }}}


    // {{{ __toString()
    /**
     * Convert the class object into a string
     *
     * @access private
     * @return string
     */
    private function __toString() {

        return $this->template;

    }
    // }}}


	// {{{ fatal()
    /**
     * This method die's whith an fatal error.
     *
     * @access private
     * @return void
     */
    private function fatal( $error, $nr, $which = NULL ) {

        if ( $which ) (string) $msg = str_replace( '%s', $which, Tmpl::$ERROR[$error] );
        else (string) $msg = Tmpl::$ERROR[$error];
        die( "<tt>Template Error in ".basename( __FILE__ )." on line ".$nr.": ".$msg."</tt>" );

    }
    // }}}


};

//**************************************************************
// EOF
?>
Return current item: Tmpl