-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Introduce way to skip fluid templates
resolves: #184 Feature if enabled replaces rendering templates in fluid in frontend context with php templates in order to simplify output json. Templates paths resolving is the same as in fluid, feature seeks just files with php extension, In php template you should just echo json_encode([...]); How to enable: - add new `headless.overrideFluidTemplates` flag to LocalConfiguration - add to typoscript settings for desired plugin, flag `phpTemplate`=1 in order to override fluid templates
- Loading branch information
1 parent
17e1fac
commit a267914
Showing
4 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the "headless" Extension for TYPO3 CMS. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.md file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace FriendsOfTYPO3\Headless\XClass; | ||
|
||
use TYPO3\CMS\Core\Http\ApplicationType; | ||
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface; | ||
use TYPO3Fluid\Fluid\View\Exception\InvalidTemplateResourceException; | ||
|
||
use function extract; | ||
use function ob_end_clean; | ||
use function ob_get_clean; | ||
use function ob_start; | ||
|
||
class TemplateView extends \TYPO3\CMS\Fluid\View\TemplateView | ||
{ | ||
public function render($actionName = null) | ||
{ | ||
if (!ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isFrontend()) { | ||
return parent::render($actionName); | ||
} | ||
|
||
$renderingContext = $this->getCurrentRenderingContext(); | ||
|
||
if ((int)($renderingContext->getVariableProvider()->get('settings')['phpTemplate'] ?? 0) !== 1) { | ||
return parent::render($actionName); | ||
} | ||
|
||
$templatePaths = $renderingContext->getTemplatePaths(); | ||
if ($actionName) { | ||
$actionName = ucfirst($actionName); | ||
$renderingContext->setControllerAction($actionName); | ||
} | ||
|
||
$templateFile = $templatePaths->resolveTemplateFileForControllerAndActionAndFormat($renderingContext->getControllerName(), $renderingContext->getControllerAction(), 'php'); | ||
|
||
if ($templateFile === null) { | ||
throw new InvalidTemplateResourceException('Template is not found'); | ||
} | ||
|
||
return $this->loadTemplate($templateFile, $renderingContext); | ||
} | ||
|
||
private function loadTemplate(string $templateFile, RenderingContextInterface $renderingContext): string | ||
{ | ||
$__jsonContent = ''; | ||
|
||
try { | ||
extract($renderingContext->getVariableProvider()->getAll()); | ||
|
||
ob_start(); | ||
include $templateFile; | ||
$__jsonContent = ob_get_clean(); | ||
} catch (\Throwable $e) { | ||
ob_end_clean(); | ||
throw $e; | ||
} | ||
|
||
return $__jsonContent; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the "headless" Extension for TYPO3 CMS. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.md file that was distributed with this source code. | ||
*/ | ||
|
||
echo json_encode([ | ||
'testKey' => $testValue, | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the "headless" Extension for TYPO3 CMS. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.md file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace FriendsOfTYPO3\Headless\Tests\Unit\XClass; | ||
|
||
use FriendsOfTYPO3\Headless\XClass\TemplateView; | ||
use TYPO3\CMS\Core\Http\ServerRequest; | ||
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext; | ||
use TYPO3\CMS\Fluid\Core\ViewHelper\ViewHelperResolver; | ||
use TYPO3\TestingFramework\Core\Unit\UnitTestCase; | ||
use TYPO3Fluid\Fluid\Core\Cache\FluidCacheInterface; | ||
use TYPO3Fluid\Fluid\Core\Variables\StandardVariableProvider; | ||
use TYPO3Fluid\Fluid\View\Exception\InvalidTemplateResourceException; | ||
use TYPO3Fluid\Fluid\View\TemplatePaths; | ||
|
||
use function json_encode; | ||
|
||
class TemplateViewTest extends UnitTestCase | ||
{ | ||
public function testTemplateNotFoundOrPHPTemplatesDisabledRender(): void | ||
{ | ||
$this->expectException(InvalidTemplateResourceException::class); | ||
|
||
$GLOBALS['TYPO3_REQUEST'] = (new ServerRequest())->withAttribute('applicationType', 1); // fe request | ||
|
||
$context = new RenderingContext($this->createMock(ViewHelperResolver::class), $this->createMock(FluidCacheInterface::class), [], []); | ||
|
||
$variableProvider = new StandardVariableProvider(); | ||
$variableProvider->add('settings', ['phpTemplate' => 1]); | ||
|
||
$context->setVariableProvider($variableProvider); | ||
$view = new TemplateView($context); | ||
$view->render(); | ||
|
||
$variableProvider = new StandardVariableProvider(); | ||
$variableProvider->add('settings', ['phpTemplate' => 0]); | ||
|
||
$context->setVariableProvider($variableProvider); | ||
$view = new TemplateView($context); | ||
$view->render(); | ||
} | ||
|
||
public function testTemplateRender(): void | ||
{ | ||
$GLOBALS['TYPO3_REQUEST'] = (new ServerRequest())->withAttribute('applicationType', 1); // fe request | ||
|
||
$context = new RenderingContext($this->createMock(ViewHelperResolver::class), $this->createMock(FluidCacheInterface::class), [], []); | ||
|
||
$variableProvider = new StandardVariableProvider(); | ||
$variableProvider->add('settings', ['phpTemplate' => 1]); | ||
$variableProvider->add('testValue', 'TestingJsonValue'); | ||
|
||
$context->setVariableProvider($variableProvider); | ||
|
||
$templatePaths = $this->createMock(TemplatePaths::class); | ||
$templatePaths->method('resolveTemplateFileForControllerAndActionAndFormat')->willReturn(__DIR__ . '/Fixtures/Templates/Default/Default.php'); | ||
|
||
$context->setTemplatePaths($templatePaths); | ||
|
||
$view = new TemplateView($context); | ||
|
||
self::assertSame(json_encode(['testKey' => 'TestingJsonValue']), $view->render()); | ||
} | ||
} |