Assets

Class.

use Saij\WpAppFramework\Asset\Assets;

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

ConstantValueDescription
CHILD_PREFIX'@child/'Prefix of the files of the active child theme's own asset directory.

Methods

image()

image(string $name, string $alt, array $attributes = []): string

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.

ParameterTypeDefaultDescription
namestringrequiredThe asset: its path below assets/, e.g. images/logo.svg, optionally with @child/ or the prefix of the instance (@theme/, @plugin/).
altstringrequiredThe alternative text; '' for a decorative image.
attributesarray<string, string|int|bool|list<int>|list<string>>[]Further attributes; true adds one without value, false leaves it out. widths (list of pixel widths) and formats ask for variants and then need sizes; src, alt and srcset cannot be set.

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()

path(string $name): string

The path of an asset in the file system.

ParameterTypeDefaultDescription
namestringrequiredThe asset: its path below assets/, e.g. images/logo.svg, optionally with @child/ or the prefix of the instance (@theme/, @plugin/).

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()

svg(string $name, array $attributes = []): string

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.

ParameterTypeDefaultDescription
namestringrequiredThe asset: its path below assets/, e.g. images/logo.svg, optionally with @child/ or the prefix of the instance (@theme/, @plugin/). The file must have the extension .svg.
attributesarray<string, string|int|bool>[]Attributes to set on the root element; true sets one without value, false removes it. class adds its classes to those of the file.

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()

url(string $name): string

The URL of an asset, versioned with a hash of its content (or, for a file of a higher layer, its modification time).

ParameterTypeDefaultDescription
namestringrequiredThe asset: its path below assets/, e.g. images/logo.svg, optionally with @child/ or the prefix of the instance (@theme/, @plugin/).

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.