Question

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}
Was it helpful?

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);

OTHER TIPS

For TYPO3 >= 7 the solution of UsmanZ needs some adjustment since templateRootPath has changed to templateRootPaths (plural)

    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);

    // From TYPO3 7 templateRootPath has changed to templateRootPaths (plural), so get the last existing template file in array (fallback)
    $templateRootPaths = $extbaseFrameworkConfiguration['view']['templateRootPaths'];
    foreach (array_reverse($templateRootPaths) as $templateRootPath) {
        $templatePathAndFilename = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($templateRootPath . $controllerName . '/' . $templateName . '.html');
        if (file_exists($templatePathAndFilename)) {
            break;
        }
    }
    $tempView->setTemplatePathAndFilename($templatePathAndFilename);

    // Set layout and partial root paths
    $tempView->setLayoutRootPaths($extbaseFrameworkConfiguration['view']['layoutRootPaths']);
    $tempView->setPartialRootPaths($extbaseFrameworkConfiguration['view']['partialRootPaths']);

    $tempView->assignMultiple($variables);
    $tempHtml = $tempView->render();

    return $tempHtml;
}

The following is more comfortable and dynamic with the plural of template-, partial- and layoutRootPaths. First parameter is only neccessary as static method is part of an utility class.

/**
 * 
 * @param \TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext $controllerContext
 * @param string $templateName
 * @param array $variables
 */
public static function render(\TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext $controllerContext, $templateName, $variables = []) {

    /* @var $objectManager \TYPO3\CMS\Extbase\Object\ObjectManager */
    $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);

    /* @var $configurationManager \TYPO3\CMS\Extbase\Configuration\ConfigurationManager */
    $configurationManager = $objectManager->get(\TYPO3\CMS\Extbase\Configuration\ConfigurationManager::class);

    /* @var $standaloneView \TYPO3\CMS\Fluid\View\StandaloneView */
    $standaloneView = $objectManager->get(\TYPO3\CMS\Fluid\View\StandaloneView::class);
    $extbaseFrameworkConfiguration = $configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);        

    $standaloneView->getRenderingContext()->setControllerContext($controllerContext);
    $standaloneView->setFormat('html');

    $standaloneView->setTemplateRootPaths($extbaseFrameworkConfiguration['view']['templateRootPaths']);
    $standaloneView->setLayoutRootPaths($extbaseFrameworkConfiguration['view']['layoutRootPaths']);
    $standaloneView->setPartialRootPaths($extbaseFrameworkConfiguration['view']['partialRootPaths']);   
    $standaloneView->setTemplate($templateName);

    $standaloneView->assignMultiple($variables);

    return $standaloneView->render();

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top