如何在没有 Extbase 的情况下渲染流体视图模板?在电子邮件模板中通过 eID

2024-01-24

我想通过 TYPO3 eID 脚本使用 Fluid 模板文件来呈现邮件正文来发送电子邮件。我找不到一种简单的方法来在正常的 MVC Extbase 上下文之外初始化 Fuid 视图。我发现的所有来源似乎都已过时且非常复杂。

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


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

/**
 * 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!'));

和流体模板typo3conf/ext/mytemplate/Resources/Private/Templates/mail.html可能看起来像这样:

Hello
{test}

随着输出

Hello
This is a test!

您需要布局和部分吗?

/**
 * 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();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在没有 Extbase 的情况下渲染流体视图模板?在电子邮件模板中通过 eID 的相关文章

随机推荐