You are here: WikiStart/Docs/Developer/Readers


Template Readers

Template readers are used to create the internal template structures from any string that contains <patTemplate:.../> tags. patTemplate ships with two readers, one that is able to read from files and one that reads directly from strings.

Reading from any datasource

If you would like to store your templates anywhere else, you can simply create a new reader. Just create a new subclass of patTemplate_Reader and place it into the reader directory. You need to implement at least one method: readTemplates(). patTemplate will call this method, whenever the user calls patTemplate::readtemplatesFromInput() and will pass the unique identifier for the template to read. This can be a filename, a URL or the value of a primary key in a database. After reading the template, you will have to return an array, that contains the template structure. In most cases you will be able to use patTemplate_Reader::parseString(), which will apply regular expressions to split the template source into several blocks and interprets all tags.

<?PHP
/**
 * patTemplate Reader that reads from a file
 *
 * @package       patTemplate
 * @subpackage    Readers
 * @author        Stephan Schmidt <schst@php.net>
 */
class patTemplate_Reader_File extends patTemplate_Reader
{
   /**
    * reader name
    * @access    private
    * @var        string
    */
    var $_name ='File';

   /**
    * read templates from any input 
    *
    * @final
    * @access    public
    * @param    string    file to parse
    * @return    array    templates
    */
    function readTemplates( $input )
    {
        $this->_currentInput = $input;
        $fullPath  = $this->_resolveFullPath( $input );
        $content   = file_get_contents( $fullPath );

        $templates = $this->parseString( $content );
        
        return $templates;
    }

   /**
    * resolve path for a template
    *
    * @access    private
    * @param    string        filename
    * @return    string        full path
    */    
    function _resolveFullPath( $filename )
    {
        $baseDir  = $this->_options['root'];
        $fullPath = $baseDir . '/' . $filename;
        return $fullPath;
    }
}?>

If you want to support the parse="off" functionality for external templates, you will have to create a second method called loadTemplate(). This method will receive a unique identifier and should return the data associated with it as a string. In the above example, you would just strip the call to parseString().

Possible readers

  • Read from database
  • Read from a template server
  • Read from shared memory

Other template engines

Readers may also be used to read templates that have been created for other template enginges than patTemplate. We already delivered a reader, that is able to read templates that have been created for HTML_Template_IT and treat them like patTemplate templates. If you want to implement a reader for other engines, you'll have to make yourself fimiliar with the internal structure of patTemplate. We'll post more information on this subject at a latter point.

Caching

patTemplate supports caching of the returned templates structures. As the caching is still in beta state and the internal plugin API may change, documentation on this topic will follow, once the API is stable enough.