Adding external frontend widgets in TYPO3

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:

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

Read more about the feature here.

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:

<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.

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.

$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 heshe can create plugins himselfherself. 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.

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.

Julia Lange

Access Current Entry in Custom Validation Rule

Sometimes, when writing a custom validation rule for a Statamic project, I find myself in a position...

Discover full article

Lakkes

Create an entry approval workflow with Statamic Revisions

When managing entries in your Statamic application that gather external data, such as from APIs, you...

Discover full article

Yamen Hadla

Effortless Language Redirection - TYPO3 Extension

Introduction Hello fellow TYPO3 enthusiasts! I had the pleasure of cooperating with my...

Discover full article
View all articles