mirror of
https://github.com/wintercms/winter.git
synced 2024-06-28 05:33:29 +02:00
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:
parent
9a13e6a53a
commit
baea29b142
@ -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;
|
||||
|
38
modules/cms/tests/classes/TwigExtensionTest.php
Normal file
38
modules/cms/tests/classes/TwigExtensionTest.php
Normal 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));
|
||||
}
|
||||
}
|
@ -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")
|
||||
;
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user