'Typo3: Add and substitute custom marker in PageRenderer

I'm using Typo3 version 10.4.6 with the following config:

config.pageRendererTemplateFile = path/to/CustomPageRendererTemplate.html

In this custom page renderer file I have added a new custom marker ###MYMARKER### inside the head section.

I need to substitute this custom marker with a specific string.

In general I need to insert a script in the head section of my page right after the opening head tag. A custom marker seems to be the only possibility render my script at the desired position.

I do not want to use the disableAllHeaderCode option!

My approach so far:

$templateService = GeneralUtility::makeInstance(MarkerBasedTemplateService::class);
$templateService->substituteMarker(
    'fileadmin/sitedesign/Resources/Private/Templates/PageRendererTemplate.html',
    '###MYMARKER###',
    '<link rel="test" content="Test content">'
);

Unfortunately the marker is not substituted.



Solution 1:[1]

Adding a <script> part can be easily done with TypoScript:

page.includeJS.myscript = /path/to/my/script.js

Also, CSS files can be added with TypoScript. More see here: https://docs.typo3.org/m/typo3/reference-typoscript/main/en-us/Setup/Page/Index.html#includejs-array

Also, you can change the <head> tag to directly add a tag directly after it, see: https://docs.typo3.org/m/typo3/reference-typoscript/main/en-us/Setup/Page/Index.html#headtag

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1