Skip to content

Commit

Permalink
[WIP][FEATURE][PoC] Introduce way to skip fluid templates
Browse files Browse the repository at this point in the history
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
twoldanski committed Feb 16, 2023
1 parent 17e1fac commit 5e5b1c3
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
64 changes: 64 additions & 0 deletions Classes/XClass/TemplateView.php
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;
}
}
8 changes: 8 additions & 0 deletions Configuration/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use FriendsOfTYPO3\Headless\Utility\HeadlessFrontendUrlInterface;
use FriendsOfTYPO3\Headless\Utility\UrlUtility;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use TYPO3\CMS\Core\Configuration\Features;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Form\Controller\FormFrontendController;
use TYPO3\CMS\FrontendLogin\Controller\LoginController;

Expand Down Expand Up @@ -44,4 +46,10 @@

$services->set(HeadlessFrontendUrlInterface::class, UrlUtility::class)->autowire(false);
$services->set(XmlSitemapRenderer::class)->public()->share(false);

$features = GeneralUtility::makeInstance(Features::class);

if ($features->isFeatureEnabled('headless.overrideFluidTemplates')) {
$services->alias(\TYPO3\CMS\Fluid\View\TemplateView::class, \FriendsOfTYPO3\Headless\XClass\TemplateView::class);
}
};

0 comments on commit 5e5b1c3

Please sign in to comment.