如何在没有Extbase的情况下呈现Fuid视图模板?即通过eID的电子邮件模板

Lud*_*wig 2 templates typo3 view fluid eid

我想使用Fluid模板文件通过TYPO3 eID脚本发送电子邮件,以呈现邮件正文。我找不到在普通的MVC Extbase上下文之外如何初始化Fuid View的简单方法。我发现的所有资料似乎都已过时且非常复杂。

那么渲染流体模板需要什么呢?

Lud*_*wig 5

这是我编写的用于渲染模板的简单函数。

/**
 * 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!'));
Run Code Online (Sandbox Code Playgroud)

而在流体模板typo3conf /转/ MyTemplate的/资源/个人/模板/ mail.html可能看起来像:

Hello
{test}
Run Code Online (Sandbox Code Playgroud)

随着输出

Hello
This is a test!
Run Code Online (Sandbox Code Playgroud)

您需要布局和局部吗?

/**
 * 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();
}
Run Code Online (Sandbox Code Playgroud)