register_asset Function
Registers a JS or CSS asset for loading within the page:
<!-- in a partial/section template, e.g. product-tile.html.twig -->
{{ register_asset('css/product-tile.css') }}
{{ register_asset('js/product-tile.js') }}
The first argument to register_asset should be the path of an asset file (JS or CSS) that will be rendered in the head of the document exactly once, even if register_asset is called with that path again (e.g. in the case of a partial being rendered multiple times, like a Product Tile).
defer
By default, register_asset loads the JS/CSS files in a deferred manner.
register_asset also takes a second argument, a JSON config object. If the defer property is set to false, the file will be loaded synchronously.
{{ register_asset('css/product-tile.css', { "defer": false }) }}
If you would like to control where the registered asset script are loaded in the DOM, use inject_registered_assets
purpose
By default, register_asset includes an HTML attribute for Square Onlineās cookie consent integration indicating the script is necessary for site functionality. For scripts that use cookies in other context (i.e. for marketing or statistics) register_asset also accepts a purpose property in its second argument to apply the correct HTML attribute.
{{ register_asset('js/analytics.js', { "purpose": "statistics" }) }}
For valid purposes, see the documentation for consent_purpose.