/** * Returns the path to the template file. * * The file locator is used to locate the template when the naming convention * is the symfony one (i.e. the name can be parsed). * Otherwise the template is located using the locator from the twig library. * * @param string|TemplateReferenceInterface $template The template * @param bool $throw When true, a \Twig_Error_Loader exception will be thrown if a template could not be found * * @return string The path to the template file * * @throws \Twig_Error_Loader if the template could not be found */ protected function findTemplate($template, $throw = true) { $logicalName = (string) $template; if (isset($this->cache[$logicalName])) { return $this->cache[$logicalName]; } $file = null; $previous = null; try { $file = parent::findTemplate($logicalName); } catch (\Twig_Error_Loader $e) { $twigLoaderException = $e; // for BC try { $template = $this->parser->parse($template); $file = $this->locator->locate($template); } catch (\Exception $e) { } } if (false === $file || null === $file) { if ($throw) { throw $twigLoaderException; } return false; } return $this->cache[$logicalName] = $file; }
/** * Returns the path to the template file. * * The file locator is used to locate the template when the naming convention * is the symfony one (i.e. the name can be parsed). * Otherwise the template is located using the locator from the twig library. * * @param string|TemplateReferenceInterface $template The template * * @throws \Twig_Error_Loader if the template could not be found * * @return string The path to the template file */ protected function findTemplate($template) { $logicalName = (string) $template; if (isset($this->cache[$logicalName])) { return $this->cache[$logicalName]; } $file = null; $previous = null; try { $template = $this->parser->parse($template); try { $file = $this->locator->locate($template); } catch (\InvalidArgumentException $e) { $previous = $e; } } catch (\Exception $e) { try { $file = parent::findTemplate($template); } catch (\Twig_Error_Loader $e) { $previous = $e; } } if (false === $file || null === $file) { throw new \Twig_Error_Loader(sprintf('Unable to find template "%s".', $logicalName), -1, null, $previous); } return $this->cache[$logicalName] = $file; }
protected function findTemplate($name) { // strip away bundle name $parts = explode(':', $name); return parent::findTemplate(end($parts)); }
public function findTemplate($name) { try { return parent::findTemplate($name . $this->extension); } catch (\Exception $e) { return parent::findTemplate($name); } }
/** * @param $name * @return mixed */ protected function findTemplate($name) { if (isset($this->cache[$name])) { return $this->cache[$name]; } if (is_file($name)) { $this->cache[$name] = $name; return $name; } return parent::findTemplate($name); }
protected function findTemplate($name) { if (null !== $this->locator) { if (isset($this->cache[$name])) { return $this->cache[$name]; } if ($path = call_user_func($this->locator, $name)) { return $this->cache[$name] = $path; } } return parent::findTemplate($name); }
/** * Do same stuff than Twig_Loader_Filesystem::exists() plus returns the file * itself if it is readable. * * @see Twig_Loader_Filesystem::findTemplate() */ protected function findTemplate($name) { try { return parent::findTemplate($name); } catch (\Twig_Error_Loader $e) { $namespace = self::MAIN_NAMESPACE; if (is_readable($name)) { return $name; } throw new \Twig_Error_Loader(sprintf('Unable to find template "%s" (looked into: %s).', $name, implode(', ', $this->paths[$namespace]))); } }
/** * @param array|string $names * @throws \Twig_Error_Loader * @return string */ protected function findTemplate($names) { if (!is_array($names)) { return parent::findTemplate($names); } foreach ($names as $name) { try { return parent::findTemplate($name); } catch (\Twig_Error_Loader $e) { } } throw new \Twig_Error_Loader(sprintf('Unable to find template "%s".', implode(', ', $names))); }
/** * {@inheritdoc} */ protected function findTemplate($name) { $real_name = realpath($name); // Make the filename relative to the template directory foreach ($this->getPaths() as $path) { $real_path = realpath($path); if (strpos($real_name, $real_path) === 0) { $name = ltrim(str_replace($real_path, '', $real_name), '/'); break; } } // Ensure that the file extension is always present if (!preg_match("/\\.{$this->extension}\$/", $name)) { $name .= ".{$this->extension}"; } return parent::findTemplate($name); }
/** * Find the template * * Override for Twig_Loader_Filesystem::findTemplate to add support * for loading from safe directories. */ protected function findTemplate($name) { $name = (string) $name; // normalize name $name = preg_replace('#/{2,}#', '/', strtr($name, '\\', '/')); // If this is in the cache we can skip the entire process below // as it should have already been validated if (isset($this->cache[$name])) { return $this->cache[$name]; } // First, find the template name. The override above of validateName // causes the validateName process to be skipped for this call $file = parent::findTemplate($name); try { // Try validating the name (which may throw an exception) parent::validateName($name); } catch (\Twig_Error_Loader $e) { if (strpos($e->getRawMessage(), 'Looks like you try to load a template outside configured directories') === 0) { // Ok, so outside of the configured template directories, we // can now check if we're within a "safe" directory // Find the real path of the directory the file is in $directory = src_realpath(dirname($file)); if ($directory === false) { // Some sort of error finding the actual path, must throw the exception throw $e; } foreach ($this->safe_directories as $safe_directory) { if (strpos($directory, $safe_directory) === 0) { // The directory being loaded is below a directory // that is "safe". We're good to load it! return $file; } } } // Not within any safe directories throw $e; } // No exception from validateName, safe to load. return $file; }
/** * Returns the path to the template file * * @param $name The template logical name * * @return string The path to the template file */ protected function findTemplate($name) { try { $tpl = $this->parser->parse($name); } catch (\Exception $e) { return parent::findTemplate($name); } if (isset($this->cache[$key = $tpl->getLogicalName()])) { return $this->cache[$key]; } $file = null; $previous = null; try { $file = $this->locator->locate($tpl); } catch (\InvalidArgumentException $e) { $previous = $e; } if (false === $file || null === $file) { throw new \Twig_Error_Loader(sprintf('Unable to find template "%s".', $tpl), -1, null, $previous); } return $this->cache[$key] = $file; }
/** * Get the final name where a template lives, regardless of whether it was called with an extension or not. * * * @param string $name The template name. * @return string The final template name. * * @throws \Twig_Error_Loader */ public function getFinalName($name) { try { return parent::findTemplate($name); } catch (\Twig_Error_Loader $e) { $exts = \App::config('TEMPLATE_EXTENSION'); if ($exts) { if (!is_array($exts)) { $exts = array($exts); } //if foreach ($exts as $e) { try { return parent::findTemplate($name . $e); } catch (\Twig_Error_Loader $e) { } } //foreach } //if } //catch throw new \Twig_Error_Loader("Twig Error Loader Exception : Could name find template `{$name}`"); }
/** * {@inheritdoc} */ protected function findTemplate($name) { return $this->loader->findTemplate($name); }
/** * Returns the path to the template file. * * The file locator is used to locate the template when the naming convention * is the symfony one (i.e. the name can be parsed). * Otherwise the template is located using the locator from the twig library. * * @param string|TemplateReferenceInterface $template The template * * @return string The path to the template file * * @throws \Twig_Error_Loader if the template could not be found */ protected function findTemplate($template) { $logicalName = (string) $template; if (isset($this->cache[$logicalName])) { return $this->cache[$logicalName]; } $file = null; $previous = null; try { $file = parent::findTemplate($logicalName); } catch (\Twig_Error_Loader $e) { throw new \Twig_Error_Loader(sprintf('Unable to find frame "%s".', $logicalName), -1, null, $previous); } return $this->cache[$logicalName] = $file; }