/** * Renders a partial. * * @param string $partialName * @param string $sectionName * @param array $variables * @param \TYPO3\CMS\Fluid\Core\ViewHelper\ViewHelperVariableContainer $viewHelperVariableContainer the View Helper Variable container to use. * @return string */ public function renderPartial($partialName, $sectionName, array $variables) { $partialNameWithFormat = $partialName . '.' . $this->controllerContext->getRequest()->getFormat(); if (!isset($this->partialIdentifierCache[$partialNameWithFormat])) { $this->partialIdentifierCache[$partialNameWithFormat] = $this->getPartialIdentifier($partialName); } $partialIdentifier = $this->partialIdentifierCache[$partialNameWithFormat]; if ($this->templateCompiler->has($partialIdentifier)) { $parsedPartial = $this->templateCompiler->get($partialIdentifier); } else { $parsedPartial = $this->templateParser->parse($this->getPartialSource($partialName)); if ($parsedPartial->isCompilable()) { $this->templateCompiler->store($partialIdentifier, $parsedPartial); } } $variableContainer = $this->objectManager->get('TYPO3\\CMS\\Fluid\\Core\\ViewHelper\\TemplateVariableContainer', $variables); $renderingContext = clone $this->getCurrentRenderingContext(); $renderingContext->injectTemplateVariableContainer($variableContainer); $this->startRendering(self::RENDERING_PARTIAL, $parsedPartial, $renderingContext); if ($sectionName !== NULL) { $output = $this->renderSection($sectionName, $variables); } else { $output = $parsedPartial->render($renderingContext); } $this->stopRendering(); return $output; }
/** * Constructor * * @param ContentObjectRenderer $contentObject The current cObject. If NULL a new instance will be created * @throws \InvalidArgumentException * @throws \UnexpectedValueException */ public function __construct(ContentObjectRenderer $contentObject = NULL) { $this->objectManager = GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager'); /** @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager */ $configurationManager = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManagerInterface'); if ($contentObject === NULL) { /** @var ContentObjectRenderer $contentObject */ $contentObject = GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer'); } $configurationManager->setContentObject($contentObject); $this->templateParser = $this->objectManager->get('TYPO3\\CMS\\Fluid\\Core\\Parser\\TemplateParser'); $this->setRenderingContext($this->objectManager->get('TYPO3\\CMS\\Fluid\\Core\\Rendering\\RenderingContext')); /** @var \TYPO3\CMS\Extbase\Mvc\Web\Request $request */ $request = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Request'); $request->setRequestURI(GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL')); $request->setBaseURI(GeneralUtility::getIndpEnv('TYPO3_SITE_URL')); /** @var \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder $uriBuilder */ $uriBuilder = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Routing\\UriBuilder'); $uriBuilder->setRequest($request); /** @var \TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext $controllerContext */ $controllerContext = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ControllerContext'); $controllerContext->setRequest($request); $controllerContext->setUriBuilder($uriBuilder); $this->setControllerContext($controllerContext); $this->templateCompiler = $this->objectManager->get('TYPO3\\CMS\\Fluid\\Core\\Compiler\\TemplateCompiler'); // singleton $this->templateCompiler->setTemplateCache(GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Cache\\CacheManager')->getCache('fluid_template')); }
/** * The compiled ViewHelper adds two new ViewHelper arguments: __thenClosure and __elseClosure. * These contain closures which are be executed to render the then(), respectively else() case. * * @param string $argumentsVariableName * @param string $renderChildrenClosureVariableName * @param string $initializationPhpCode * @param \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\AbstractNode $syntaxTreeNode * @param \TYPO3\CMS\Fluid\Core\Compiler\TemplateCompiler $templateCompiler * @return string * @internal */ public function compile($argumentsVariableName, $renderChildrenClosureVariableName, &$initializationPhpCode, \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\AbstractNode $syntaxTreeNode, \TYPO3\CMS\Fluid\Core\Compiler\TemplateCompiler $templateCompiler) { foreach ($syntaxTreeNode->getChildNodes() as $childNode) { if ($childNode instanceof \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\ViewHelperNode && $childNode->getViewHelperClassName() === 'TYPO3\\CMS\\Fluid\\ViewHelpers\\ThenViewHelper') { $childNodesAsClosure = $templateCompiler->wrapChildNodesInClosure($childNode); $initializationPhpCode .= sprintf('%s[\'__thenClosure\'] = %s;', $argumentsVariableName, $childNodesAsClosure) . chr(10); } if ($childNode instanceof \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\ViewHelperNode && $childNode->getViewHelperClassName() === 'TYPO3\\CMS\\Fluid\\ViewHelpers\\ElseViewHelper') { $childNodesAsClosure = $templateCompiler->wrapChildNodesInClosure($childNode); $initializationPhpCode .= sprintf('%s[\'__elseClosure\'] = %s;', $argumentsVariableName, $childNodesAsClosure) . chr(10); } } return \TYPO3\CMS\Fluid\Core\Compiler\TemplateCompiler::SHOULD_GENERATE_VIEWHELPER_INVOCATION; }
/** * The compiled ViewHelper adds two new ViewHelper arguments: __thenClosure and __elseClosure. * These contain closures which are be executed to render the then(), respectively else() case. * * @param string $argumentsVariableName * @param string $renderChildrenClosureVariableName * @param string $initializationPhpCode * @param \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\AbstractNode $syntaxTreeNode * @param \TYPO3\CMS\Fluid\Core\Compiler\TemplateCompiler $templateCompiler * @return string * @internal */ public function compile($argumentsVariableName, $renderChildrenClosureVariableName, &$initializationPhpCode, \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\AbstractNode $syntaxTreeNode, \TYPO3\CMS\Fluid\Core\Compiler\TemplateCompiler $templateCompiler) { foreach ($syntaxTreeNode->getChildNodes() as $childNode) { if ($childNode instanceof ViewHelperNode && $childNode->getViewHelperClassName() === ThenViewHelper::class) { $childNodesAsClosure = $templateCompiler->wrapChildNodesInClosure($childNode); $initializationPhpCode .= sprintf('%s[\'__thenClosure\'] = %s;', $argumentsVariableName, $childNodesAsClosure) . LF; } if ($childNode instanceof ViewHelperNode && $childNode->getViewHelperClassName() === ElseViewHelper::class) { $childNodesAsClosure = $templateCompiler->wrapChildNodesInClosure($childNode); $initializationPhpCode .= sprintf('%s[\'__elseClosure\'] = %s;', $argumentsVariableName, $childNodesAsClosure) . LF; } } return sprintf('%s::renderStatic(%s, %s, $renderingContext)', get_class($this), $argumentsVariableName, $renderChildrenClosureVariableName); }
/** * This ViewHelper is used a *lot* because it is used by the escape interceptor. * Therefore we render it to raw PHP code during compilation * * @param string $argumentsVariableName * @param string $renderChildrenClosureVariableName * @param string $initializationPhpCode * @param \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\AbstractNode $syntaxTreeNode * @param \TYPO3\CMS\Fluid\Core\Compiler\TemplateCompiler $templateCompiler * @return string */ public function compile($argumentsVariableName, $renderChildrenClosureVariableName, &$initializationPhpCode, \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\AbstractNode $syntaxTreeNode, \TYPO3\CMS\Fluid\Core\Compiler\TemplateCompiler $templateCompiler) { $valueVariableName = $templateCompiler->variableName('value'); $initializationPhpCode .= sprintf('%s = (%s[\'value\'] !== NULL ? %s[\'value\'] : %s());', $valueVariableName, $argumentsVariableName, $argumentsVariableName, $renderChildrenClosureVariableName) . LF; return sprintf('(!is_string(%s) ? %s : htmlspecialchars(%s, (%s[\'keepQuotes\'] ? ENT_NOQUOTES : ENT_COMPAT), (%s[\'encoding\'] !== NULL ? %s[\'encoding\'] : \\TYPO3\\CMS\\Fluid\\Core\\Compiler\\AbstractCompiledTemplate::resolveDefaultEncoding()), %s[\'doubleEncode\']))', $valueVariableName, $valueVariableName, $valueVariableName, $argumentsVariableName, $argumentsVariableName, $argumentsVariableName, $argumentsVariableName); }