TYPO3

Fluid components in TYPO3 v14 without a PHP class

5 min read

Fluid components, no PHP

When I rebuilt hoja.in on TYPO3 14 this year, the templates were the part I was most nervous about. The old site was TYPO3 7.6 with flux, and the markup lived in a jungle of partials with arguments="{_all}" everywhere. You open a partial and you have no idea which variables it expects. You find out by breaking it.

This time I wanted small, named building blocks: a button, a card, a timeline entry. Each one should say clearly what it needs. Fluid components do exactly that, and since TYPO3 v14.1 you don’t even need a PHP class to register them. The whole theme of this site now runs on them.

Partials vs. components, in one sentence

A partial is a piece of template you call with <f:render partial="..."> and hope you passed the right variables. A component is a piece of template you call like a ViewHelper, <hoja:molecule.card title="...">, and it declares its arguments with <f:argument>, so Fluid checks them for you.

Components came with Fluid 4.3. In TYPO3 v13 you had to write a small ComponentCollection class that extends AbstractComponentCollection and returns the template paths. In v14 (Fluid 5 is installed in my project) there is a configuration file for it. Honestly, that’s the version I prefer: less code, nothing to autoload.

Solution: components in a site package

Let’s say your site package has the extension key sitepackage and the PHP namespace HarvisoftSitepackage, like mine.

Step 1: Register the collection in Configuration/Fluid/ComponentCollections.php:

<?php

declare(strict_types=1);

return [
    'Harvisoft\Sitepackage\Components' => [
        'templatePaths' => [
            10 => 'EXT:sitepackage/Resources/Private/Components',
        ],
    ],
];

The array key is a PHP namespace, but there is no class behind it. It only acts as the identifier of the collection and as the Fluid namespace.

Step 2: Give it a short global prefix in Configuration/Fluid/Namespaces.php, so templates don’t need an xmlns line at the top. This file is also new in v14.1 and replaces $GLOBALS['TYPO3_CONF_VARS']['SYS']['fluid']['namespaces'], which is deprecated now:

<?php

declare(strict_types=1);

return [
    'hoja' => ['Harvisoft\Sitepackage\Components'],
];

Step 3: Create the components. By default every component gets its own folder, and the file name repeats the folder name. So <hoja:atom.button> resolves to Components/Atom/Button/Button.fluid.html. It looks redundant first, but it leaves space to put the CSS or a small JS file next to the template later.

Here is the button from this site:

<f:comment>Call-to-action link in the theme's button styles.</f:comment>
<f:argument name="label" type="string" />
<f:argument name="link" type="string" />
<f:argument name="variant" type="string" optional="{true}" default="signal" description="signal or ghost" />

<p class="button-row">
    <f:link.typolink parameter="{link}" class="button button--{variant}">{label}</f:link.typolink>
</p>

And a card that takes its text from the content between the tags, via <f:slot />:

<f:argument name="title" type="string" />
<f:argument name="link" type="string" optional="{true}" />

<li>
    <h3><f:if condition="{link}">
        <f:then><f:link.typolink parameter="{link}">{title}</f:link.typolink></f:then>
        <f:else>{title}</f:else>
    </f:if></h3>
    <p><f:slot /></p>
</li>

Step 4: Use them in any template. A card grid, for example, is an organism that wraps the cards:

<hoja:organism.cardGrid title="Services">
    <hoja:molecule.card title="TYPO3 upgrades" link="t3://page?uid=12">
        From old classic-mode installs to v14, step by step.
    </hoja:molecule.card>
    <hoja:molecule.card title="Extension development">
        Extbase, PSR-14 events, tests included.
    </hoja:molecule.card>
</hoja:organism.cardGrid>

Flush the caches and that’s it. No PHP class, no TypoScript.

Things that surprised me

The strict arguments are the best part, and also the first thing that bites. If you pass an argument the component does not declare, Fluid throws an exception. If you really want a collection that accepts anything, there is 'additionalArgumentsAllowed' => true in the collection config. I left it off. The error is the feature.

The slot escapes HTML that comes from a variable. If you write HTML directly between the tags, it stays HTML. But <hoja:molecule.card title="x">{someHtml}</hoja:molecule.card> is escaped, because Fluid can’t know where that string came from. The Fluid docs suggest f:format.raw on the variable when you pass it in, and only for content you trust, like RTE fields that already went through TYPO3’s HTML sanitizer. I like that it is a decision now and not an accident.

Named slots exist too. With <f:fragment name="..."> on the calling side and <f:slot name="..." /> in the component, you can fill several areas, for example a media column and a text column. I haven’t needed it yet on this site, so I can’t tell you much more than the docs.

The folder pattern can be changed. If Button/Button.fluid.html annoys you, set 'templateNamePattern' => '{path}/{name}' and the component lives directly in Atom/Button.fluid.html.

Autocompletion in the IDE

A nice little extra from v14: the schema command now includes components.

ddev exec vendor/bin/typo3 fluid:schema:generate

It writes *.xsd files to var/transient/. Point PhpStorm at them, and <hoja: suggests your components with their arguments. For a theme with twenty components this saves a lot of jumping between files.

Supporting v13 and v14 in one extension

If you ship an extension for both versions, you can keep a class-based collection for v13 and add the configuration file for v14. The changelog says that in v14 the configuration wins and the class is ignored completely. So the v13 class is only a compatibility shim, and you can delete it when you drop v13.

My take

For years I treated partials as the way to structure Fluid, and I never really liked them. Components feel like what partials should have been from the beginning: a name, a list of arguments and a body. It was the first time I built a TYPO3 theme where I could open any template and immediately understand what it renders.

If you try components in your project, drop a comment with how you organised the folders. I’m still not sure atoms, molecules and organisms is the right split for a small site like mine.

Further reading

Leave a comment

Your email address will not be published.