Benutzer: Gast
Eintrag: Extbase 6+: Standalone-Template-Renderer

Extbase 6+: Standalone-Template-Renderer

von: marcel
Tags: Fluid, Typo3, Extbase
Kreise: Typo3

Method in Controller:

Code: PHP
/**
 * Creates a fluid instance with given template-file and controller-settings
 * @param string $file Path below Template-Root-Path (Resources/Private/Templates/$file)
 * @return \TYPO3\CMS\Fluid\View\StandaloneView Fluid-Template-Renderer
 */
protected function getFluidRenderer($file = 'Email/Email.txt') {
  // create another instance of Fluid
  $renderer = $this->objectManager->get('TYPO3\\CMS\\Fluid\\View\\StandaloneView');
  #$renderer->setFormat("html");
  $renderer->setControllerContext($this->controllerContext);
   // find the view-settings and set the template-files
  $conf = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
  $renderer->setTemplatePathAndFilename(\TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($conf['view']['templateRootPath']) . $file);
  $renderer->setLayoutRootPath(\TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($conf['view']['layoutRootPath']));
  $renderer->setPartialRootPath(\TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($conf['view']['partialRootPath']));

  // and return the new Fluid instance
  return $renderer;
}

Call in Action:

Code: PHP
$renderer = $this->getFluidRenderer("Form/Email.html");
$renderer->assignMultiple($variables);
$html = $renderer->render();

Hier noch eine erweiterte Variante, um bei Bedarf alle Pfade beim Aufruf der Methode angeben zu können:

Code: PHP
/**
 * Creates a fluid instance with template-file and controller-settings.
 * Additionally it is possible to set paths to layout- und partial-folders.
 * @param \string $file Path below Template-Root-Path
 * @param \string $templateRootPathAndFilename
 * @param \string $layoutRootPath
 * @param \string $partialRootPath
 * @param \boolean $forceFile If set to true, $templateRootPathAndFilename will be recognized as "templateRootPath" without filename. $file is forced to be used.
 * @return \TYPO3\CMS\Fluid\View\StandaloneView Fluid-Template-Renderer
 */
protected function getFluidRenderer($file, $templateRootPathAndFilename = "", $layoutRootPath = "", $partialRootPath = "", $forceFile = false) {
  // create another instance of Fluid
  $renderer = $this->objectManager->get('TYPO3\\CMS\\Fluid\\View\\StandaloneView');
  $renderer->setControllerContext($this->controllerContext);

  // find the view-settings, if template, layout or partial is not set
  if (!($templateRootPathAndFilename && $layoutRootPath && $partialRootPath)) {
    $conf = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
  }

  // set template and filename
  if ($templateRootPathAndFilename) {
    if ($forceFile) {
      $renderer->setTemplatePathAndFilename(\TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($templateRootPathAndFilename) . $file);
    } else {
      $renderer->setTemplatePathAndFilename(\TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($templateRootPathAndFilename));
    }
  } else {
    $renderer->setTemplatePathAndFilename(\TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($conf['view']['templateRootPath']) . $file);
  }

  // set layouts-folder
  if ($layoutRootPath) {
    $renderer->setLayoutRootPath(\TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($layoutRootPath));
  } else {
    $renderer->setLayoutRootPath(\TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($conf['view']['layoutRootPath']));
  }

  // set partials-folder
  if ($partialRootPath) {
    $renderer->setPartialRootPath(\TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($partialRootPath));
  } else {
    $renderer->setPartialRootPath(\TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($conf['view']['partialRootPath']));
  }

  return $renderer;
}
(1) kaiszy mo schrieb am 10. 9. 2013 um 17:08 Uhr
Klasse Snippet! Scheint aber nicht zu gehen wenn ich den Controller, der den Standalone-View rendern soll per "$this->objectManager->get" instanziert habe. Weisst Du woran das liegt ?