Location: PHPKode > projects > ExTemplates Library > TemplateFactory.php
<?

/*
This script is a part of the ExTemplates Library
Author: Alexander Netkachev

Check the license.txt for copyright and terms.
*/


class TemplateFactory {
  var $cacheTemplates;
  var $templatesFolder;
  var $cacheFolder;

  function TemplateFactory() {
    $this->cacheTemplates = true;
    $this->templatesFolder = './';
    $this->cacheFolder = 'tplswap/';
  }

  function & getTemplate($name) {
    $tplSwapFilename = $this->cacheFolder . $name . '.swp';
    if ($this->cacheTemplates && file_exists($tplSwapFilename) && filemtime($name) == filemtime($tplSwapFilename)) {
      return unserialize(implode('', file($tplSwapFilename)));
    } else if (file_exists($name)) {
      $tpl = null;
      $tb = new TemplateBuilder();
      $tpl = & $tb->buildTemplateFromString(implode('', file($name)));
      if ($this->cacheTemplates) {
        touch($tplSwapFilename);
        if (!$tplSwap = fopen($tplSwapFilename, 'w'))
          exit ('Cannot open template swap file ' . $tplSwapFilename);
        if (!fwrite($tplSwap, serialize($tpl)))
          exit ('Cannot write to template swap file ' . $tplSwapFilename);
        fclose($tplSwap);
        touch($tplSwapFilename, filemtime($name));
      }
      return $tpl;
    } else
        exit ('Failed to load template file ' . $filename .'. File not found.');
  }
}

?>
Return current item: ExTemplates Library