Adding external frontend widgets in TYPO3
Tags: typo3
Published: 2021-10-04 07:06:16

Sometimes we or our clients want to include external JavaScript widgets from e. g. mobile.de or some real estate platform showing their ratings. This is totally fine, but how to implement those scripts the best way? Well, first of all: There is no perfect way but different ways in TYPO3 that all have their purpose.

## Fluid-way

Since TYPO3 8.6 Fluid provides two sections provided by the core to add data to `<head>` and above the ending `</body>`. This way is usable both in static content elements known as Fluid styled content (FSC) as well as in action templates which is a great benefit. It also does not require any developer to add the widgets to more pages, if the editor can create and move elements. Take a look at how easy it is:

```html
<f:section name="FooterAssets">
    <script async src="//www.mobile.de/bewertungen/ratingwidget.js?dealerId=1234"></script>
</f:section>
```

Read more about the feature [here](https://docs.typo3.org/c/typo3/cms-core/master/en-us/Changelog/8.6/Feature-79409-AutorenderAssetSectionsInFluidTemplateWithController.html).

There is also another way, by utilizing the `AssetCollector` introduced with TYPO3 10.3 You simply can pass the path to the local source or direct content into the `f:asset` viewhelper like so:

```html
<f:asset.css identifier="identifier123" href="EXT:my_ext/Resources/Public/Css/foo.css" />
<f:asset.css identifier="identifier123">
   .foo { color: black; }
</f:asset.css>
```

Read more about the feature [here](https://docs.typo3.org/c/typo3/cms-core/master/en-us/Changelog/10.3/Feature-90522-IntroduceAssetCollector.html).

## TypoScript-way

A more static way to implement external libraries is TypoScript. The following code snippet will inject the library only on pages with the uid 1 and 4. While this works perfectly fine, it needs manual effort to add this script to more pages and also needs a developer if it needs to be removed.

```
mainPage = PAGE
mainPage { … }

[getTSFE().id in [1,4]]
    mainPage.headerData.250 = TEXT
    mainPage.headerData.250.value = <script async src="//www.mobile.de/bewertungen/ratingwidget.js?dealerId=1234"></script>
[end]
```

## Extbase-way

Just like the Fluid-way, Extbase provides the ability to add data to the `<head>` and above the `</body>` by adding items to the parsed TypoScript.

```php
$GLOBALS['TSFE']->additionalFooterData['ratingwidget'] = '<script async src="//www.mobile.de/bewertungen/ratingwidget.js?dealerId=456306"></script>'
```

This is great, because developers does not have to manually add this data when an editor wants it on other pages, because he*she can create plugins himself*herself. It’s also backend driven meaning we don’t add extra data inside a view or template. This is also negative, because we write HTML in PHP code. Since TYPO3 10.3 there is a solution. Calling the above mentioned `AssetCollector` directly.

```php
use TYPO3\CMS\Core\Page\AssetCollector;
use TYPO3\CMS\Core\Utility\GeneralUtility;

GeneralUtility::makeInstance(AssetCollector::class)
   ->addJavaScript('my_ext_foo', 'EXT:my_ext/Resources/Public/JavaScript/foo.js', ['data-foo' => 'bar']);
```

These are several ways we use to include external JS widgets and styles for our and our customers websites. What way(s) do you use the most? I prefer the Fluid-way.