craft-snippets / Craft-image-toolbox

Craft CMS plugin that helps with the use of image transforms in templates.
Other
16 stars 0 forks source link

Variables in attributes with Transform Layouts #9

Closed artmem closed 1 year ago

artmem commented 2 years ago

I'm using transform layouts, and want to set alt text for my images using using my image.alt tag or image.title, but there doesn't seem to be a way to do it with transform layouts. I can't put the variables in the settings, so I tried

            {% set image = entry.heroImage.one() %}
            {% set attributes ={
                alt: image.alt ? image.alt : image.title
            } %}
            {{craft.images.layout(image, 'wide',  attributes)}}

and it outputs the image as expected, but just ignores the attributes. Is there a way to accomplish this?

piotrpog commented 1 year ago

@artmem Althought some time has passed, this functionality is now included :) You can now pass anonymous function to attributes array property in layouts settings. This function can have $assets variable which will contain assets used in this layout. More info: https://craftsnippets.com/docs/image-toolbox/Placeholders.html