CMS Twig extension contentFunction() should return bool like partialFunction() (#746)

This allows people to use the `content` function as a conditional to add fallback content if a given content file does not exist.
This commit is contained in:
Marc Jauvin 2022-11-14 00:47:56 -05:00 committed by GitHub
parent 9a13e6a53a
commit baea29b142
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 7 deletions

View File

@ -1098,10 +1098,11 @@ class Controller
*
* @param string $name The content view to load.
* @param array $parameters Parameter variables to pass to the view.
* @throws SystemException If the content cannot be found
* @return string
* @param bool $throwException Throw an exception if the content file is not found.
* @throws SystemException If the content cannot be found, and `$throwException` is true.
* @return string|false Content file, or false if `$throwException` is false.
*/
public function renderContent($name, $parameters = [])
public function renderContent($name, $parameters = [], $throwException = true)
{
/**
* @event cms.page.beforeRenderContent
@ -1127,7 +1128,11 @@ class Controller
* Load content from theme
*/
elseif (($content = Content::loadCached($this->theme, $name)) === null) {
throw new SystemException(Lang::get('cms::lang.content.not_found_name', ['name'=>$name]));
if ($throwException) {
throw new SystemException(Lang::get('cms::lang.content.not_found_name', ['name' => $name]));
} else {
return false;
}
}
$fileContent = $content->parsedMarkup;

View File

@ -0,0 +1,38 @@
<?php
namespace Cms\Tests\Classes;
use Cms\Twig\Extension;
use Cms\Classes\Controller;
use System\Tests\Bootstrap\TestCase;
use Winter\Storm\Exception\SystemException;
class TwigExtensionTest extends TestCase
{
public function testPartialFunction()
{
$extension = new Extension;
$controller = Controller::getController() ?: new Controller;
$extension->setController($controller);
$this->assertFalse($extension->partialFunction('invalid-partial-file', [], false));
$this->expectException(SystemException::class);
$this->expectExceptionMessageMatches('/is\snot\sfound/');
$this->assertFalse($extension->partialFunction('invalid-partial-file', [], true));
}
public function testContentFunction()
{
$extension = new Extension;
$controller = Controller::getController() ?: new Controller;
$extension->setController($controller);
$this->assertFalse($extension->contentFunction('invalid-content-file', [], false));
$this->expectException(SystemException::class);
$this->expectExceptionMessageMatches('/is\snot\sfound/');
$this->assertFalse($extension->contentFunction('invalid-content-file', [], true));
}
}

View File

@ -39,6 +39,7 @@ class ContentNode extends TwigNode
->write("echo \$this->env->getExtension('Cms\Twig\Extension')->contentFunction(")
->subcompile($this->getNode('nodes')->getNode(0))
->write(", \$context['__cms_content_params']")
->write(", true")
->write(");\n")
;

View File

@ -101,7 +101,7 @@ class Extension extends TwigExtension
/**
* Renders the requested partial with the provided parameters. Optionally throw an exception if the partial cannot be found
*/
public function partialFunction(string $name, array $parameters = [], bool $throwException = false): string
public function partialFunction(string $name, array $parameters = [], bool $throwException = false): string|bool
{
return $this->controller->renderPartial($name, $parameters, $throwException);
}
@ -109,9 +109,9 @@ class Extension extends TwigExtension
/**
* Renders the requested content file.
*/
public function contentFunction(string $name, array $parameters = []): string
public function contentFunction(string $name, array $parameters = [], bool $throwException = false): string|bool
{
return $this->controller->renderContent($name, $parameters);
return $this->controller->renderContent($name, $parameters, $throwException);
}
/**