如何在Controller中获取模板的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}
答案 0 :(得分: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 :(得分: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;
}
答案 2 :(得分:0)
以下对于模板,partial-和layoutRootPaths的复数形式更加舒适和动态。仅当静态方法是实用程序类的一部分时,才需要第一个参数。
/**
*
* @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();
}