Skip to content
Grav 2.0 is officially stable. Read the announcement →
General

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

Started by Jared 8 years ago · 3 replies · 603 views
8 years ago

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.

TWIG
    {% 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).

8 years ago

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:

TWIG
{{ 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
8 years ago

You could try to replace

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

by

TWIG
{{ 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 :) )

8 years ago

Thanks @apotropaic That works perfectly!

Suggested topics

Topic Participants Replies Views Activity
General · by Hanns Mattes, 3 weeks ago
1 268 3 weeks ago
General · by Andy Miller, 3 weeks ago
0 196 3 weeks ago
General · by Jerry Hunt, 3 weeks ago
2 332 3 weeks ago
General · by pamtbaau, 3 weeks ago
1 253 3 weeks ago
General · by Andy Miller, 3 weeks ago
0 226 3 weeks ago