This documentation describes unreleased changes on the main branch.

asset_image()

Twig function of the framework; its result is HTML and is printed without escaping.

{{ asset_image(name, alt, attributes = {}) }}

An img element for an image asset with src, width and height (if known, so the page does not jump while it loads), alt, decoding="async" and the given attributes. With widths and sizes the element gets a srcset of smaller versions; with formats it is wrapped in a picture element with a source per format. The build creates these variants from the calls in the templates, so the name, widths and formats must be literals there. The output is HTML and is printed unescaped. An unknown or invalid name, or variants the build did not create, are errors.

{{ asset_image('images/hero.jpg', 'Our team', {
    widths: [640, 1280, 1920],
    sizes: '(min-width: 1200px) 1200px, 100vw',
    formats: ['avif', 'webp'],
    class: 'w-full h-auto',
}) }}
ParameterTypeDefaultDescription
namestringrequiredPath of the asset below assets/, e.g. images/logo.svg; it may start with @child/ or with the instance's own prefix (@theme/ or @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. src, srcset and alt cannot be set. widths (list of pixel widths, needs sizes) and formats (avif, webp, jpeg, png) ask for variants.

Returns string