Using Fluid in TYPO3
Here are some examples of how Fluid can be used in TYPO3:
- Create a template (theme) using a combination of TypoScript FLUIDTEMPLATE and Fluid. Check out the TYPO3 site package tutorial which walks you through the creation of a sitepackage extension.
- Create a custom content element type in addition to the already existing content elements TYPO3 supplies.
- Extbase-based controllers have a default Fluid
view in
$this->view
. - Use Fluid to create emails using the TYPO3 Mail API.
- Use Fluid in backend modules, either with or without Extbase.
- Use the generic view factory to create a Fluid view.
Deprecated since version 13.3
These classes have been marked as deprecated in TYPO3 v13 and will be removed in v14:
Using the generic view factory (ViewFactoryInterface)
New in version 13.3
Class \TYPO3\
has been added as a
generic view factory interface to create views that return an instance of
\TYPO3\
. This implements the "V" of "MVC"
in a generic way and is used throughout the TYPO3 core.
You can inject an instance of the
\TYPO3\
to create an instance of a
\TYPO3\
where you need one.
Note
Extbase-based controllers create a view
instance based on this factory by default and which is accessible as
$this->view
.
<?php
namespace MyVendor\MyExtension\Controller;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\View\ViewFactoryData;
use TYPO3\CMS\Core\View\ViewFactoryInterface;
final readonly class MyController
{
public function __construct(
private ViewFactoryInterface $viewFactory,
) {}
public function myAction(ServerRequestInterface $request): string
{
$viewFactoryData = new ViewFactoryData(
templateRootPaths: ['EXT:my_extension/Resources/Private/Templates'],
partialRootPaths: ['EXT:my_extension/Resources/Private/Partials'],
layoutRootPaths: ['EXT:my_extension/Resources/Private/Layouts'],
request: $request,
);
$view = $this->viewFactory->create($viewFactoryData);
$view->assign('mykey', 'myValue');
return $view->render('path/to/template');
}
}
The View
needs an instance of
\TYPO3\
, which is a data object and should
therefore be created via new
.
Best practices in creating a View
instance:
- Hand over request of type
\Psr\
if possible. See Getting the PSR-7 request object.Http\ Message\ Server Request Interface - Use the tuple
$template
,Root Paths $partial
andRoot Paths $layout
if possible by providing an array of "base" paths likeRoot Paths 'EXT:
my_ extension/ Resources/ Private/ (Templates |Partials |Layouts)' - Avoid using parameter
$template
Path And Filename - Call
render
without file-ending on the returned ViewInterface instance.('path/ within/ template Root Path')