Reputation: 71
I am using the StandaloneView to output some data.
The problem I encountered is that the given TemplateRootPath is just ignored.
TYPO3 throws an exception saying that it looked for the template in
/typo3/public/typo3conf/ext//Resources/Private/Templates/
but the given TemplateRootPath is one folder further down in /printView/.
Maybe there is something that went over my head but I am looking for a fix for about one and a half hours now.
$standaloneView = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Fluid\View\StandaloneView::class);
$standaloneView->setLayoutRootPaths([
\TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName('EXT:<my-extension>/Resources/Private/Layouts/printView/'),
]);
$standaloneView->setPartialRootPaths([
\TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName('EXT:<my-extension>/Resources/Private/Partials'),
]);
$standaloneView->setTemplateRootPaths([
\TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName('EXT:<my-extension>/Resources/Private/Templates/printView/'),
]);
$standaloneView->setFormat('html');
$standaloneView->setTemplate('printView/printView');
$standaloneView->assignMultiple([
//variable assignment
]);
echo $standaloneView->render();
<my-extension> is there on purpose
Upvotes: 0
Views: 774
Reputation: 55798
It's little bit late to the party, but as you already has the paths of your private resources declared within the TypoScript of your ext, you can just read them instead of rewriting (DRY!).
with imports
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\View\StandaloneView;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
Just declare a method somewhere in your controller (to retrieve its context):
private function initializeStandAloneView(): StandaloneView
{
/** @var StandaloneView $view */
$view = GeneralUtility::makeInstance(StandaloneView::class);
// As you already has the paths of your private resources declared within the TypoScript, you can just read them instead of rewriting (DRY!)
$cfg = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
$view->setControllerContext($this->controllerContext); // important part
$view->setLayoutRootPaths($cfg['view']['templateRootPaths']);
$view->setTemplateRootPaths($cfg['view']['templateRootPaths']);
$view->setPartialRootPaths($cfg['view']['partialRootPaths']);
return $view;
}
So finally you can use it in action(s) of sample My
controller to render many different standalone views easily like:
/**
* Some action in MyController.php
*/
public function jumperAction()
{
$view = $this->initializeStandAloneView();
if (1 == 1){
$view->assign('foo', 'bar');
return $view->render('Jumper/EverythingOk');
}
return $view->render('Jumper/NotFound');
}
finally you can create these views in files:
typo3conf/ext/yourext/Resources/Private/Templates/My/Jumper/EverythingOk.html
typo3conf/ext/yourext/Resources/Private/Templates/My/Jumper/NotFound.html
and really finally, don't forget to clear the cache :D
Upvotes: 0
Reputation: 10791
set...RootPaths(...)
expects an array as parameter.
Maybe you need to change the filename of the template to UpperCamelCase.
That might not be needed for the parameter you use for selection, but the generated filename will have it. (PrintView.html
instead of printView.html
)
Upvotes: 0