statamic / ideas

💡Discussions on ideas and feature requests for Statamic
https://statamic.dev
30 stars 1 forks source link

Asset Fieldtype: access to Asset Field object #1026

Open jimblue opened 11 months ago

jimblue commented 11 months ago

Hello gentlement !

To explain my point the best is a simple exemple so here it is.

For a gallery_images Asset Fieldtype that returns an array of assets I have the following template:

{# gallery template #}
{{ gallery_images }}
    <img src="{{ url }}" alt="{{ alt }}" width="{{ width }}" height="{{ height }}" />
{{ /gallery_images }}

Now I want to use an Antlers "component" to create the image:

{# image component #}
<img src="{{ url }}" alt="{{ alt }}" width="{{ width }}" height="{{ height }}" />
{# gallery template #}
{{ gallery_images }}
    {{ partial:common/components/image :url="url" :alt="alt" :width="width" :height="height" }}
{{ /gallery_images }}

This is working but it's far from ideal as I have to declare every single arguments (there is a lot more of them ...) It could be so much simpler if we could pass the image Asset object like so:

{# image component #}
<img src="{{ image:url }}" alt="{{ image:alt }}" width="{{ image:width }}" height="{{ image:height }}" />
{# gallery template #}
{{ gallery_images }}
    {{ partial:common/components/image :image="value" }}
{{ /gallery_images }}

I've use a {{ value }} tag in the example to follow the same nomenclature used in array.

duncanmcclean commented 11 months ago

Does this work at all?

{{ gallery_images scope="image" }}
    {{ partial:common/components/image :image="image" }}
{{ /gallery_images }}
jimblue commented 11 months ago

Actually, it's possible to do:

{{ gallery_images }}
      {{ partial:common/components/image :image="gallery_images[index]" }}
{{ /gallery_images }}

Thanks @edalzell !

But I still think it could be a good idea to use {{ value }} to respect use same nomenclature as array.

jimblue commented 11 months ago

Does this work at all?

{{ gallery_images scope="image" }}
    {{ partial:common/components/image :image="image" }}
{{ /gallery_images }}

Just tried and indeed its working ! This is a little cleaner than using the index thank you. 🙏🏼

But I still think that using the same predictable usage for array and assets by giving access to a {{ value }} field would be nicer 😺