From dca6128501890473043cbe2bfd3016b4f48d39d6 Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Fri, 11 Sep 2020 15:47:49 -0600 Subject: [PATCH] Change Twig template loading fallbacks Previously: - registered Laravel view file - attempt to load file as a CMS partial Now: - registered Laravel view file - valid CMS partials - any file that Twig can access (from the project root) rendered as a plain twig template (but with support for the CMS twig environment) Fixes https://github.com/octobercms/library/commit/80aab47f044a2660aa352450f55137598f362aa4#commitcomment-42223643, https://github.com/octobercms/october/issues/5261#issuecomment-691235167 --- modules/cms/twig/Loader.php | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/modules/cms/twig/Loader.php b/modules/cms/twig/Loader.php index d3fe3475b..c0b1dc10a 100644 --- a/modules/cms/twig/Loader.php +++ b/modules/cms/twig/Loader.php @@ -27,7 +27,9 @@ class Loader extends LoaderBase implements TwigLoaderInterface /** * Sets a CMS object to load the template from. + * * @param \Cms\Contracts\CmsObject $obj Specifies the CMS object. + * @return void */ public function setObject(CmsObject $obj) { @@ -37,6 +39,9 @@ class Loader extends LoaderBase implements TwigLoaderInterface /** * Returns the Twig content string. * This step is cached internally by Twig. + * + * @param string $name The template name + * @return TwigSource */ public function getSourceContext($name) { @@ -65,6 +70,9 @@ class Loader extends LoaderBase implements TwigLoaderInterface /** * Returns the Twig cache key. + * + * @param string $name The template name + * @return string */ public function getCacheKey($name) { @@ -77,6 +85,10 @@ class Loader extends LoaderBase implements TwigLoaderInterface /** * Determines if the content is fresh. + * + * @param string $name The template name + * @param mixed $time The time to check against the template + * @return bool */ public function isFresh($name, $time) { @@ -89,6 +101,9 @@ class Loader extends LoaderBase implements TwigLoaderInterface /** * Returns the file name of the loaded template. + * + * @param string $name The template name + * @return string */ public function getFilename($name) { @@ -101,6 +116,9 @@ class Loader extends LoaderBase implements TwigLoaderInterface /** * Checks that the template exists. + * + * @param string $name The template name + * @return bool */ public function exists($name) { @@ -115,11 +133,12 @@ class Loader extends LoaderBase implements TwigLoaderInterface * Internal method that checks if the template name matches * the loaded object, with fallback support to partials. * + * @param string $name The template name to validate * @return bool */ protected function validateCmsObject($name) { - if ($name === $this->obj->getFilePath()) { + if ($this->obj && $name === $this->obj->getFilePath()) { return true; } @@ -133,18 +152,29 @@ class Loader extends LoaderBase implements TwigLoaderInterface /** * Looks up a fallback CMS partial object. - * @return Cms\Classes\Partial + * + * @param string $name The filename to attempt to load a fallback CMS partial for + * @return Cms\Classes\Partial|bool Returns false if a CMS partial can't be found */ protected function findFallbackObject($name) { + // Ignore Laravel views if (strpos($name, '::') !== false) { return false; } + // Check the cache if (array_key_exists($name, $this->fallbackCache)) { return $this->fallbackCache[$name]; } - return $this->fallbackCache[$name] = CmsPartial::find($name); + // Attempt to load the path as a CMS Partial object + try { + $partial = CmsPartial::find($name); + } catch (\Exception $e) { + return false; + } + + return $this->fallbackCache[$name] = $partial; } }