How to render a Fuid view template without Extbase? I. e an email template by eID

心已入冬 提交于 2019-11-30 19:55:57

问题


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:


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();
}


来源:https://stackoverflow.com/questions/25772722/how-to-render-a-fuid-view-template-without-extbase-i-e-an-email-template-by-ei

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!