Example #1
0
 /**
  * Check if the template exist and compile it if necessary
  *
  * @param string $template: name of the file of the template
  *
  * @throw \Rain\Tpl\NotFoundException the file doesn't exists
  * @return string: full filepath that php must use to include
  */
 protected function checkTemplate($template)
 {
     // set filename
     $templateName = basename($template);
     $templateBasedir = strpos($template, '/') !== false ? dirname($template) . '/' : null;
     $templateDirectory = null;
     $templateFilepath = null;
     $parsedTemplateFilepath = null;
     // Make directories to array for multiple template directory
     $templateDirectories = $this->config['tpl_dir'];
     if (!is_array($templateDirectories)) {
         $templateDirectories = array($templateDirectories);
     }
     $isFileNotExist = true;
     // absolute path
     if ($template[0] == '/') {
         $templateDirectory = $templateBasedir;
         $templateFilepath = $templateDirectory . $templateName . '.' . $this->config['tpl_ext'];
         $parsedTemplateFilepath = $this->config['cache_dir'] . $templateName . "." . md5($templateDirectory . serialize($this->config['checksum'])) . '.rtpl.php';
         // For check templates are exists
         if (file_exists($templateFilepath)) {
             $isFileNotExist = false;
         }
     } else {
         foreach ($templateDirectories as $templateDirectory) {
             $templateDirectory .= $templateBasedir;
             $templateFilepath = $templateDirectory . $templateName . '.' . $this->config['tpl_ext'];
             $parsedTemplateFilepath = $this->config['cache_dir'] . $templateName . "." . md5($templateDirectory . serialize($this->config['checksum'])) . '.rtpl.php';
             // For check templates are exists
             if (file_exists($templateFilepath)) {
                 $isFileNotExist = false;
                 break;
             }
         }
     }
     // if the template doesn't exsist throw an error
     if ($isFileNotExist === true) {
         $e = new Tpl\NotFoundException('Template ' . $templateName . ' not found!');
         throw $e->templateFile($templateFilepath);
     }
     // Compile the template if the original has been updated
     if ($this->config['debug'] || !file_exists($parsedTemplateFilepath) || filemtime($parsedTemplateFilepath) < filemtime($templateFilepath)) {
         $parser = new Tpl\Parser($this->config, static::$plugins, static::$registered_tags);
         $parser->compileFile($templateName, $templateBasedir, $templateDirectory, $templateFilepath, $parsedTemplateFilepath);
     }
     return $parsedTemplateFilepath;
 }
Example #2
0
 /**
  * Check if the template exist and compile it if necessary
  *
  * @param string $template: name of the file of the template
  *
  * @throw \Rain\Tpl\NotFoundException the file doesn't exists
  * @return string: full filepath that php must use to include
  */
 protected function checkTemplate($template)
 {
     // fix dummy windows when include template with a sub folder
     $template = str_replace(array("\\b", "\f", "\n", "\\p", "\r"), array(DIRECTORY_SEPARATOR . 'b', DIRECTORY_SEPARATOR . 'f', DIRECTORY_SEPARATOR . 'n', DIRECTORY_SEPARATOR . 'p', DIRECTORY_SEPARATOR . 'r'), $template);
     // set filename
     $templateName = basename($template);
     $templateBasedir = strpos($template, DIRECTORY_SEPARATOR) ? dirname($template) . DIRECTORY_SEPARATOR : null;
     $templateDirectory = null;
     $templateFilepath = null;
     $parsedTemplateFilepath = null;
     // Make directories to array for multiple template directory
     $templateDirectories = $this->config['tpl_dir'];
     if (!is_array($templateDirectories)) {
         $templateDirectories = array($templateDirectories);
     }
     $isFileNotExist = true;
     foreach ($templateDirectories as $templateDirectory) {
         $templateDirectory .= $templateBasedir;
         $templateFilepath = $templateDirectory . $templateName . '.' . $this->config['tpl_ext'];
         $parsedTemplateFilepath = $this->config['cache_dir'] . $templateName . "." . md5($templateDirectory . serialize($this->config['checksum'])) . '.rtpl.php';
         // For check templates are exists
         if (file_exists($templateFilepath)) {
             $isFileNotExist = false;
             break;
         }
     }
     // if the template doesn't exsist throw an error
     if ($isFileNotExist === true) {
         $e = new Tpl\NotFoundException('Template ' . $templateName . ' not found!');
         throw $e->templateFile($templateFilepath);
     }
     // Compile the template if the original has been updated
     if ($this->config['debug'] || !file_exists($parsedTemplateFilepath) || filemtime($parsedTemplateFilepath) < filemtime($templateFilepath)) {
         $parser = new Tpl\Parser($this->config, static::$plugins, static::$registered_tags);
         $parser->compileFile($templateName, $templateBasedir, $templateDirectory, $templateFilepath, $parsedTemplateFilepath);
     }
     return $parsedTemplateFilepath;
 }