Libraries can be attached to all pages, a subset of pages, or to elements in a render array. This allows you to have some assets that are global, and others that are only loaded on an as-needed basis.
Note: While the example below covers how to attach a library in a theme, the same steps work for modules. In the case of modules you'll most likely want to use #attached
as part of a Render Array.
- Attach a library globally to all pages
- Attach a library to a specific page
- Use #attached to add a library conditionally via a preprocess function
- Attach a library from a Twig template