-
-
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.
[WIP][FEATURE][PoC] 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 5e5b1c3
Showing
2 changed files
with
72 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,64 @@ | ||
<?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 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'); | ||
|
||
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