/** * Resolve template path. * @return string */ public function path() { if (is_null($this->folder)) { $path = $this->engine->getDirectory() . DIRECTORY_SEPARATOR . $this->file; } else { $path = $this->folder->getPath() . DIRECTORY_SEPARATOR . $this->file; if (!is_file($path) and $this->folder->getFallback() and is_file($this->engine->getDirectory() . DIRECTORY_SEPARATOR . $this->file)) { $path = $this->engine->getDirectory() . DIRECTORY_SEPARATOR . $this->file; } } if (!is_null($this->engine->getFileExtension())) { $path .= '.' . $this->engine->getFileExtension(); } return $path; }
/** * Render a string * * @param string $template The template content to render * @param array $locals The variable to use in template * @return null|string */ public function render($template, array $locals = array()) { $tmpName = uniqid('plates_tmp_', false); $tmpPath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $tmpName; if (!is_null($this->plates->getFileExtension())) { $tmpPath .= '.' . $this->plates->getFileExtension(); } $isTmpRegister = $this->plates->getFolders()->exists(sys_get_temp_dir()); if (!$isTmpRegister) { $this->plates->addFolder(sys_get_temp_dir(), sys_get_temp_dir()); } file_put_contents($tmpPath, $template); $data = $this->plates->render(sys_get_temp_dir() . '::' . $tmpName, $locals); unlink($tmpPath); if (!$isTmpRegister) { $this->plates->getFolders()->remove(sys_get_temp_dir()); } return $data; }
/** * Set the parsed template file. * @param string $file * @return Name */ public function setFile($file) { if ($file === '') { throw new LogicException('The template name "' . $this->name . '" is not valid. ' . 'The template name cannot be empty.'); } $this->file = $file; if (!is_null($this->engine->getFileExtension())) { $this->file .= '.' . $this->engine->getFileExtension(); } return $this; }