template module

From: Date: Thu, 02 Aug 2001 20:10:28 +0000
Subject: template module
Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-1241@lists.php.net to get a copy of this message
I propose to use a template manager, like there is a log manager. I already realized something, but we could add some methods in the abstract class. What do you think? It should already work with IT and ITX, but that would allow other template system to be plugged in <?php // $Id: Template.php,v 1.2 2001/07/04 15:56:05 shuther Exp $ /** * The Template:: class implements both an abstraction for various template * mechanisms. * * @author Jon Parise <jon@csh.rit.edu> * @author Stephane Huther <shuther@bigfoot.com> * @version $Revision: 1.0 $ */ class Template {
    // {{{ factory()
    /**
     * Attempts to return a concrete Template instance of $template_type.
     *
     * @param $template_type The type of concrete Template subclass to return.
     *                  Attempt to dynamically include the code for this
     *                  subclass. Currently, valid values are 'it' and
     *                  'itx'.
     *
     * @param  string @root File root directory, prefix for all filenames
     *                  given to the object.
     *
     * @return          The newly created concrete Log instance, or an
     *                  false on an error.
     */
    function &factory ($template_type, $root = '') {
        $template_type = strtolower($template_type);
        $classfile = 'HTML/' . $template_type . '.php';
@include_once $classfile; switch($template_type)
	    {
case 'it': $class='IntegratedTemplate'; break; case 'itx': $class='IntegratedTemplateExtension'; break; default: $class = 'HTML_' . $template_type;
	    }
if (class_exists($class)) {
            return new $class($root);
        } else {
            return false;
        }
    }
    // }}}
    // {{{ singleton()
    /**
     * Attempts to return a reference to a concrete Template instance of
     * $template_type, only creating a new instance if no template instance
     * with the same parameters currently exists.
     *
     * You should use this if there are multiple places you might
     * create a tepmlate instance, you don't want to create multiple templates
     * systems, and you don't want to check for the existance of one each time.
     * The singleton pattern does all the checking work for you.
     *
     * <b>You MUST call this method with the $var = &Template::singleton()
     * syntax. Without the ampersand (&) in front of the method name,
     * you will not get a reference, you will get a copy.</b>
     *
     * @param string $template_type The type of concrete Template subclass
     *                  to return. Attempt to dynamically include the
     *                  code for this subclass. Currently, valid values are
     *                  'it' and 'itx'.
     *
     * @param  string @root File root directory, prefix for all filenames
     *                  given to the object.
     * @return          The concrete Template reference, or false on an error.
     */
    function &singleton ($template_type, $root = '') {
        static $instances;
        if (!isset($instances)) $instances = array();
        $signature = md5($template_type . '][' . $root);
        if (!isset($instances[$signature])) {
            $instances[$signature] = Template::factory($template_type, $root);
        }
        return $instances[$signature];
    }
} ?>

« previous php.pear.dev (#1241) next »