The TagRenderer service is used to generate the HTML <script> and <link> tags required to load your Webpack Encore entrypoints in Twig templates or directly in PHP.
It automatically handles asset paths, integrity hashes (if available), and allows for custom attributes. You can specify a specific entrypoint name (defaulting to _default) and provide extra attributes like async or defer via the $extraAttributes array.
// To render <script> tags for a JavaScript entrypoint
$scriptTags = $tagRenderer->renderWebpackScriptTags(
'app', // entryName
null, // packageName (optional)
'_default', // entrypointName (optional)
['defer' => true] // extraAttributes
);
// To render <link> tags for a CSS entrypoint
$linkTags = $tagRenderer->renderWebpackLinkTags(
'app', // entryName
null, // packageName (optional)
'_default', // entrypointName (optional)
['class' => 'js-app-style'] // extraAttributes
);