Search code examples
fluidextbasetypo3-6.1.x

Getting HTML of Fluid Template in Controller Action in Typo3


How can I get HTML of a template in my Controller for a specific action.

for Example if I have two actions in one controllers

/**
 * action question
 *
 * @return void
 */
public function questionAction() {}

/**
 * action Answer
 *
 * @return void
 */
public function answerAction() { // here I've needed html code of questionAction's template}

Solution

  • Try this function to get any fluid template html.

        public function getTemplateHtml($controllerName, $templateName, array $variables = array()) {
            /** @var \TYPO3\CMS\Fluid\View\StandaloneView $tempView */
            $tempView = $this->objectManager->get('TYPO3\\CMS\\Fluid\\View\\StandaloneView');
    
            $extbaseFrameworkConfiguration = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
    
            $templateRootPath = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($extbaseFrameworkConfiguration['view']['templateRootPath']);
            $templatePathAndFilename = $templateRootPath . $controllerName . '/' . $templateName . '.html';
    
            $tempView->setTemplatePathAndFilename($templatePathAndFilename);
    
            $tempView->assignMultiple($variables);
            $tempHtml = $tempView->render();
    
            return $tempHtml;
        }
    

    Like in your example, you can call this in your answerAction like:

    $this->getTemplateHtml($controllerName, 'question', $optMarkers);