How to render a Fuid view template without Extbase

2020-03-06 05:05发布

I want to send an email by a TYPO3 eID script using a Fluid template file to render the mail body. I could not find a simple way how to initialize a Fuid View outside of the normal MVC Extbase context. All sources I found seemed to be outdated and very complex.

So what is needed to render a fluid template?

1条回答
叛逆
2楼-- · 2020-03-06 06:05

Here is a simple function I wrote to render my templates.

/**
 * Renders the fluid email template
 * @param string $template
 * @param array $assign
 * @return string
 */
public function renderFluidTemplate($template, Array $assign = array()) {
    $templatePath = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName('EXT:myextension/Resources/Private/Templates/' . $template);

    $view = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Fluid\\View\\StandaloneView');
    $view->setTemplatePathAndFilename($templatePath);
    $view->assignMultiple($assign);

    return $view->render();
}

echo renderFluidTemplate('mail.html', array('test' => 'This is a test!'));

And the fluid template in typo3conf/ext/mytemplate/Resources/Private/Templates/mail.html could look like that:

Hello
{test}

With the output

Hello
This is a test!

You Need Layouts and Partials?

/**
 * Returns the rendered fluid email template
 * @param string $template
 * @param array $assign
 * @param string $ressourcePath
 * @return string
 */
public function renderFluidTemplate($template, Array $assign = array(), $ressourcePath = NULL) {
    $ressourcePath = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($ressourcePath === NULL ? 'EXT:myextension/Resources/Private/' : $ressourcePath);

    /* @var $view \TYPO3\CMS\Fluid\View\StandaloneView */
    $view = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Fluid\\View\\StandaloneView');
    $view->setLayoutRootPath($ressourcePath . 'Layouts/');
    $view->setPartialRootPath($ressourcePath . 'Partials/');
    $view->setTemplatePathAndFilename($ressourcePath . 'Templates/' . $template);
    $view->assignMultiple($assign);

    return $view->render();
}
查看更多
登录 后发表回答