2014-01-29 30 views
3

如何在特定操作中獲取我的Controller中模板的HTML。在Typo3中獲取控制器操作中流體模板的HTML

例如,如果我有兩個動作在一個控制器

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

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

回答

4

試試這個功能讓任何液體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; 
    } 

就象在你例如,可以在answerAction像調用此:

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

$ this-> view-> render(「question」);也是我找到的一個解決方案。但你的工作也很棒。 –

+0

是的,它也可以工作,但是如果你在同一個控制器中。 – UsmanZ

1

對於TYPO3> = 7 UsmanZ的解決方案需要自templateRootPath一些調整已經變爲templateRootPaths(複數)

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