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 80aab47f04 (commitcomment-42223643), https://github.com/octobercms/october/issues/5261#issuecomment-691235167
This commit is contained in:
parent
9dca130fae
commit
dca6128501
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue