You can attach libraries in templates, for example html.html.twig
is an ideal place to attach libraries conditional on the path. You can either use template suggestions, e.g. html--front.html.twig
or a twig expression.
The other answers revolve around preprocess, which is fine, however preprocess is an abstraction and makes theming development more opaque, i.e. it buries important information in a PHP function, rather than being obvious and clear in the template.
I'm not saying preprocess is bad, only that it depends largely on how you want to work and how opaque/abstract you want to theme.
To attach a library only for the front page in html.html.twig
we can use the not
operator and the root_path
variable as a condition:
{{ not root_path ? attach_library('my_theme/my_library') }}
If you create the template suggestion html--front.html.twig
you can attach the library unconditionally:
{{ attach_library('my_theme/my_library') }}