Re: Play nice! Re: Savant/Flexy/Foo template engines

From: Date: Thu, 10 Jun 2004 14:41:59 +0000
Subject: Re: Play nice! Re: Savant/Flexy/Foo template engines
References: 1 2 3 4 5 6 7 8 9 10 11  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-30398@lists.php.net to get a copy of this message
I have started an attempt on the Jausions interface. So far as I can tell, it is free of Savant-isms like plugins and filters, and free of Flexy-isms as well.
    http://wiki.ciaweb.net/yawiki/index.php?area=Savant&page=TemplateSource
I have added what I think are minor modifications to the Jausions interface, to wit: * Data assigned to the template is split into two properties, one for variables ($_vars) and one for references ($_refs). This is because they must be extracted differently: extract() only honors references back to the object property, not all the way back to the business logic. To the developer, it should "look like" there is only one storage space for both vars and refs. More on this later, when we see the setData, setDataByRef, clearData, and toString methods. * There is a property for the MIME type, $_mime. Not sure if it's really needed, it gets set in openTemplate(). * There is a property for the output generated by toString(), $_output, so that it may be stored properly within toString() without possibly overwriting a reference. Better reference-checking may obviate this. * There is a property for a PEAR_ErrorStack object, $err, so that errors can be reported and checked on the Template object itself. Error reporting in this first attempt is of necessity almost nonexistent, although I note where errors should be generated. ---- My understanding of the Jausions interface is that the usage is similar to the following: * Construct a template object * Set data * Open a template source * Output the template For example: $tpl =& new Template(); $tpl->setData(...); $tpl->openTemplate($file); $tpl->output(); CODE NOTES: * The openTemplate() method seems the logical place to extend pre-filter, compile, and post-filter operations (Flexy), as well as multiple path locations (Savant). As long as the method sets $this->_tpl (the source reference) to an include()-able stream or file, extended methods should be able to handle any kind of source, whether object, stream, text, or file. * The setData() method accepts three types of variable setting (are there any other kinds than these?): # assign-by-object, where the object properties are assigned to the template # assign-by-array, where the key-value pairs are assigned to the template # assign-by-name, where a single value is assigned by name to the template * setDataByRef() only assigns by name, not sure it makes sense to support the by-object and by-array types of assignment, but I could be wrong * clearData() extends the Jausions interface somewhat by allowing you to clear all values, one value by name, or a series of values listed in an array. Note that it clears both refs and vars; we want the interface to "look like" one storage unit to developers. * toString() extracts vars and refs into the local namespace differently. This bears some explanation. Let's say there is only one property ($_data) that would store all assigned data, whether by value or by reference. If we then extract($this->_data), the references extracted only go back to $_data, not back to the script that set the reference in the first place; thus, if the assigned reference changes in the calling script after it is assigned, it will not be honored within the template. As such, we must have different extraction methods for vars and refs; thus, the two separate properties, and the extra code to make it "look like" (to the developer) that there is a single storage location within the template. * An extended toString() seems the logical place for output filters to be applied. * toFile() is copied directly from the Jausions code, not sure it fits well here. I can see very easily how Savant and Flexy would both extend from this minimalist base class and still maintain their "native" APIs in the extended versions. E.g., in Savant (this is not everything) -- class Template_Savant extends Template { function assign() { return $this->setData(func_get_args()); } function fetch($tpl = null) { if (! is_null($tpl)) { $this->openTemplate($tpl); } $output = $this->toString(); foreach ($this->filter as $obj) { $obj->filter($output); } return $output; } function display($tpl = null) { echo $this->fetch($tpl); } } -- Paul M. Jones Savant: the simple alternative to Smarty for PHP. http://phpsavant.com/ DB_Table: build RDBMS tables and XHTML forms in one PHP class. http://wiki.ciaweb.net/yawiki/index.php?area=DB_Table Yawiki: your collaborative online documentation system. http://yawiki.com/ Yawp: a single-file foundation for PHP applications. http://phpyawp.com/

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