Page.media.images outputs blank title and alt fields in some cases

Hi
I’m not sure if this is a bug or me doing something stupid. I’m setting my alt and title tags via fields in the admin panel. When I’ve got one image per page things work fine with code like this
{{ page.media.images[header.image].html('{{ header.image_title }}', '{{ header.image_alt }}', 'img-responsive') }}

I don’t know if its the loop or just nothing being a header field or what but when I’ve got multiple images and I loop through something like this I get empty alt and title fields (class is hard coded on the page), no errors nothing.

    {% for pt in page.header.pricetext %}
     {{ page.media.images[pt.img].html('{{ pt.image_title }}', '{{ header.image_alt }}', 'img-responsive') }}
     {% endfor %}

The code is virtually identical. I can do {{ pt.images_title }} anywhere else and get values just fine so its not bad data. I can even put pt.img in place or pt.images_title and no output of that either.

I haven’t tested with the class tag as its always just hardcoded on there. I’ve seen this happen in a couple of different places around the site so it seems like a system thing (or me being repeatedly dumb).

I believe the issue here is just a syntax error. You have already tags opening {{ }} so you don’t need to repeat those. Try this in your loop:

{{ page.media.images[pt.img].html( pt.image_title , header.image_alt', 'img-responsive') }}

Notice the lack of quotes and {{ }} around your twig variables. I think that’ll work for you.

1 Like

You could try to replace

     {{ page.media.images[pt.img].html('{{ pt.image_title }}', '{{ header.image_alt }}', 'img-responsive') }}

by

{{ page.media.images[pt.img].html(~ pt.image_title ~, ~ header.image_alt ~, 'img-responsive') }}

(this should only apply if the solution @apotropaic provided doesn’t work :slight_smile: )

Thanks @apotropaic That works perfectly!