Re: [Fwd: Re: [PEAR-DEV] MVC revisted]

From: Date: Tue, 11 May 2004 20:51:39 +0000
Subject: Re: [Fwd: Re: [PEAR-DEV] MVC revisted]
References: 1 2 3 4 5 6  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-29118@lists.php.net to get a copy of this message
That won't work, as noted by someone else, the implementation details are too different to get a common API for all template engines (though it is a noble concept). All my MVC framework would do is provide a handle to an instance of which ever template engine you want to use. Not terribly powerful other than you can assume your template engine has been instantiated by the time your view gets executed. --Tony I just need to figure out the best way to set the $configVals, which hold which template engine to instantiate, Philippe Jausions wrote:
Tony Bibbs wrote:
For example adding an optional template factory that supports any of
the PEAR-based template libraries is on my to-do list. Funny that you said that, because I started a thread last week about PEAR supporting/recommending standard interfaces for packages. I don't think having a factory is a good thing here because the engines are so different in their inner working and a factory would have to limit capabilities. My approach was to ask the package mainteners to comply with a PEAR-community approved interface. Since PHP5 helps code interfaces I think it's a good way to go. In a framework a template engine should be seen as a black box. We feed a template and data, and we ask for the result. We should care if the engine uses blocks, tags, pseudo-code or if the template is a string, a file or taken from a database. Here's a preview of what I'm writing (that's work in progress, I can't even guarantee that it would parse properly ;-) -Philippe <?php interface Interface_ContentGenerating {
    abstract public function openTemplate($template);
    abstract public function setData($name, $data = null);
    abstract public function setDataByRef($name, &$data);
    abstract public function output();
    abstract public function toFile($filename);
    abstract public function toString();
} class HTML_Template_???Interfaced extends HTML_Template_??? implements Interface_ContentGenerating {
    function HTML_Template_???Interfaced() {
        $this->__construct();
    }
    function __construct() {
        parent::__construct();
    }
    public function openTemplate($template) {
    }
    public function setData($name, $data = null) {
    }
    public function setDataByRef($name, &$data) {
    }
    public function output() {
    }
    public function toFile($filename) {
        $file = @fopen($filename, 'wb');
        if (!$file) {
            require_once('PEAR.php');
            return PEAR::raiseError('Couldn\'t open file to write template result to.');
        }
        $mgc = get_magic_quotes_runtime();
        set_magic_quotes_runtime(0);
        $length = @fwrite($file, $this->toString());
        set_magic_quotes_runtime($mgc);
        if ($length === false) {
            require_once('PEAR.php');
            return PEAR::raiseError('Couldn\'t write template result to file.');
        }
        if (!@fclose($file)) {
            require_once('PEAR.php');
            return PEAR::raiseError('Couldn\'t close file with template result.');
        }
        return $length;
    }
    public function toString() {
        ob_start();
        $this->output();
        return ob_get_clean();
    }
} ?>


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