Assets
Class.
The static files of the instance's assets/ directory: their URL, path,
an <img> element or an inline SVG. The Twig functions asset_url(),
asset_image() and asset_svg() call this service; inject it where PHP
needs a file, for example in a pattern or a mail.
An asset is named by its path below assets/, e.g. images/logo.svg. A
higher layer replaces it with a file of the same name in its overrides
directory: a child theme in overrides/{theme}/assets/, a theme or child
theme in overrides/{plugin}/assets/. @child/… names a file of the
active child theme's own assets/ directory, which needs no counterpart in
the theme; @theme/… or @plugin/… names the instance's own file and
ignores overrides.
Constants
Methods
image()
An <img> element for an image: src, width and height (so the page does not jump while it loads), alt,
decoding="async" and further attributes. With widths and sizes the element gets a srcset of smaller
versions; with formats (e.g. ['avif', 'webp']) it is wrapped in a <picture> with a <source> per format. The
build creates these variants from the literal calls of asset_image() in the templates.
Returns string
Throws InvalidArgumentException: If the name is invalid or names no asset, or an attribute is invalid.
Throws LogicException: If the variants the attributes ask for were not built, or the name starts with @child/ but no child theme is active.
path()
The path of an asset in the file system.
Returns string
Throws InvalidArgumentException: If the name is invalid or names no asset.
Throws LogicException: If the name starts with @child/ but no child theme is active.
svg()
The markup of an SVG image, to place it inline (for example icons styled with CSS), with further attributes on its root element; classes are added to its own.
Returns string
Throws InvalidArgumentException: If the asset is no SVG image or has no root element <svg>, or the name is invalid or names no asset.
url()
The URL of an asset, versioned with a hash of its content (or, for a file of a higher layer, its modification time).
Returns string
Throws InvalidArgumentException: If the name is invalid or names no asset.
Throws LogicException: If the name starts with @child/ but no child theme is active.