Statamic - How can I access assets and single assets with attributes?

1.3k views Asked by At

In statamic, I have a container with images defined, and attributes set on those images. I then save a single asset page variable in page/index.md

photo: assets/main/image.jpg

I can use the method below, but only if I create a single image fieldset for the page, which is tedious.

{{ asset:photo }}
    {{ glide src="{{ url }}" alt="{{ alt }}" />
{{ /asset:photo }}

{{ asset:photo }}
    <img src="{{ value }}" alt="{{ alt }}" />
{{ /asset:photo }}

I can also make a container and put the single image in the container to access like below, but that is also tedious.

{{ assets container="photo" }}
    <img src="{{ glide:id height="44" fit="crop_focal" }}" alt="{{ alt }}" />
{{ /assets }}

What is the best way to access single assets with their attributes without creating a container or fieldset every time? Bonus if I can also use glide to manipulate the image!

I resorted to using the below method, but cannot access any attributes this way.

<img src="{{ glide:photo height='44' fit='resize' }}" alt="Have to enter this attribute manually" />
1

There are 1 answers

0
eYinka On

I believe this is what you need:

Create an Asset field type, for this example, we can name it featured_image. Then set the max_files to 1.

In your Antlers template, call it like this:

<img src="{{ featured_image }}" alt="{{ alt }}">

or

<img src="{{ featured_image:permalink }}" alt="{{ alt }}">

I hope this helps anyone struggling with it.