skip to Main Content

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 = TYPO3CMSCoreUtilityGeneralUtility::makeInstance(TYPO3CMSFluidViewStandaloneView::class);
$standaloneView->setLayoutRootPaths([
    TYPO3CMSCoreUtilityGeneralUtility::getFileAbsFileName('EXT:<my-extension>/Resources/Private/Layouts/printView/'),
]);
$standaloneView->setPartialRootPaths([
    TYPO3CMSCoreUtilityGeneralUtility::getFileAbsFileName('EXT:<my-extension>/Resources/Private/Partials'),
]);
$standaloneView->setTemplateRootPaths([
    TYPO3CMSCoreUtilityGeneralUtility::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

2

Answers


  1. 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)

    Login or Signup to reply.
  2. 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 TYPO3CMSCoreUtilityGeneralUtility;
    use TYPO3CMSFluidViewStandaloneView;
    use TYPO3CMSExtbaseConfigurationConfigurationManagerInterface;
    

    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 😀

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search