/** * Loads a template. * * @param ehough_templating_TemplateReferenceInterface $template A template * * @return ehough_templating_storage_Storage|bool false if the template cannot be loaded, a ehough_templating_storage_Storage instance otherwise */ public function load(ehough_templating_TemplateReferenceInterface $template) { $key = hash('sha256', $template->getLogicalName()); $dir = $this->dir . DIRECTORY_SEPARATOR . substr($key, 0, 2); $file = substr($key, 2) . '.tpl'; $path = $dir . DIRECTORY_SEPARATOR . $file; if (is_file($path)) { if (null !== $this->logger) { $this->logger->debug(sprintf('Fetching template "%s" from cache', $template->get('name'))); } elseif (null !== $this->debugger) { // just for BC, to be removed in 3.0 $this->debugger->log(sprintf('Fetching template "%s" from cache', $template->get('name'))); } return new ehough_templating_storage_FileStorage($path); } if (false === ($storage = $this->loader->load($template))) { return false; } $content = $storage->getContent(); if (!is_dir($dir)) { mkdir($dir, 0777, true); } file_put_contents($path, $content); if (null !== $this->logger) { $this->logger->debug(sprintf('Storing template "%s" in cache', $template->get('name'))); } elseif (null !== $this->debugger) { // just for BC, to be removed in 3.0 $this->debugger->log(sprintf('Storing template "%s" in cache', $template->get('name'))); } return new ehough_templating_storage_FileStorage($path); }
/** * Loads a template. * * @param ehough_templating_TemplateReferenceInterface $template A template * * @return ehough_templating_storage_Storage|bool false if the template cannot be loaded, a ehough_templating_storage_Storage instance otherwise * * @api */ public function load(ehough_templating_TemplateReferenceInterface $template) { $file = $template->get('name'); if (self::isAbsolutePath($file) && is_file($file)) { return new ehough_templating_storage_FileStorage($file); } $replacements = array(); foreach ($template->all() as $key => $value) { $replacements['%' . $key . '%'] = $value; } $logs = array(); foreach ($this->templatePathPatterns as $templatePathPattern) { if (is_file($file = strtr($templatePathPattern, $replacements)) && is_readable($file)) { if (null !== $this->logger) { $this->logger->debug(sprintf('Loaded template file "%s"', $file)); } elseif (null !== $this->debugger) { // just for BC, to be removed in 3.0 $this->debugger->log(sprintf('Loaded template file "%s"', $file)); } return new ehough_templating_storage_FileStorage($file); } if (null !== $this->logger || null !== $this->debugger) { $logs[] = sprintf('Failed loading template file "%s"', $file); } } foreach ($logs as $log) { if (null !== $this->logger) { $this->logger->debug($log); } elseif (null !== $this->debugger) { $this->debugger->log($log); } } return false; }
public function load(ehough_templating_TemplateReferenceInterface $template) { if (method_exists($this, $method = 'get' . ucfirst($template->get('name')) . 'Template')) { return new ehough_templating_storage_StringStorage($this->{$method}()); } return false; }
public function load(ehough_templating_TemplateReferenceInterface $template) { if (isset($this->templates[$template->getLogicalName()])) { return new ehough_templating_storage_StringStorage($this->templates[$template->getLogicalName()]); } return false; }