TYPO3 设置控制器操作的模板视图



我想将列表操作的视图模板用于我的 listByYear 操作。我尝试了setTemplatePathAndFilename但没有成功。它仍然找不到模板。

抱歉,未找到请求的视图。

技术原因是:未找到模板。无法查看 已解决在课堂上"按年列出"的操作 "XXX\YYY\控制器\事件控制器"。

/**
* action listByYear
* @param XXXYYYDomainModelEvent $event
*
* @return void
*/
public function listByYearAction(XXXYYYDomainModelEvent $event)
{
$date = $event->getStart();
$events = $this->eventRepository->findByYear($date->format('Y'));
$this->view->setTemplatePathAndFilename(
'typo3conf/ext/' .
$this->request->getControllerExtensionKey() .
'/Resources/Private/Templates/Event/List.html'
);
debug('typo3conf/ext/' .
$this->request->getControllerExtensionKey() .
'/Resources/Private/Templates/Event/List.html');
$this->view->assign('events', $events);
}

如何使其使用列表的模板?

非常简短的回答是,你不能。视图已经初始化,并要求在操作触发之前解析模板,实际上早在您可以影响它将查找的模板文件名的任何时间点之前。

按照约定将解析的模板文件必须始终存在。这就是允许控制器操作呈现的原因。然后,您可以通过设置模板名称(操作(来覆盖模板文件,但我不建议您这样做。

总体建议:使用默认模板命名逻辑。如果需要重用模板,请考虑重构需要重用的模板部件,将它们放置在部分模板中。

// Do not forget the use in the header ..., 
// or write fully qualified class path..
use TYPO3CMSCoreUtilityGeneralUtility;
use TYPO3CMSFluidViewStandaloneView;

// then add something like this in your action before the assign...
// or maybe create a Standalone view: search the web for "Extbase Standaloneview"
// have a look at: /typo3/sysext/about/Classes/Controller/AboutController.php
$this->view = GeneralUtility::makeInstance(StandaloneView::class);
$this->view->setTemplate('ActionName');
$this->view->setTemplateRootPaths(['EXT:your_ext/Resources/Private/Templates']);
$this->view->setPartialRootPaths(['EXT:your_ext/Resources/Private/Partials']);
$this->view->setLayoutRootPaths(['EXT:your_ext/Resources/Private/Layouts']);
$this->view->assignMultiple([
'whatever' => $whatever,
'youLike' => $youLike,
]);

最新更新