Forum: Ruby on Rails DRY question - image_tag :alt and :title the same

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D39d7c8071ed8bed6b851c64697b1a29?d=identicon&s=25 Oliver Paulus (paoli)
on 2007-02-28 12:46
(Received via mailing list)
Hello,

I have the following:
image_tag('show.png', :alt => 'show article', :title => 'show
article', :border => 0)

Is it possible that I donĀ“t need to have "=> 'show article'" twice in
this row? e.g. something similar to this pseudo code: (:alt, :title)
=> 'show article'.
70225136eacd3d870f64e03bff678655?d=identicon&s=25 Russell Norris (Guest)
on 2007-03-02 14:44
(Received via mailing list)
Short version: As far as tooltips go, :alt = IE and :title = FF. You'll
need
both to display a tooltip across browsers. There _are_ differences
between
them but I'm not sure if you're looking for that information so much as
"do
I need both". :)

RSL
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (au5lander)
on 2007-03-02 16:28
(Received via mailing list)
short answer, no there is no way to specify the value of 2 attributes
the way you describe.

you could write a helper tho

def my_image_tag(source, options = {})
  options.symbolize_keys!

  # if there is an alt attribute but no title attribute
  options[:title] = options[:alt] if options.has_key?(:alt) &&
!options.has_key?(:title)

  # if there is a title attribute but no alt attribute
  options[:alt] = options[:title] if options.has_key?(:title) &&
!options.has_key?(:alt)

  # don't change anything if both or none are specified

  image_tag(source, options)
end

not tested, but should work, examples below

<%= my_image_tag(..., { :alt => 'alt tag' }) %>
<img src="..." alt="alt tag", title="alt tag"/>

<%= my_image_tag(..., { :title => 'title tag' }) %>
<img src="..." alt="title tag", title="title tag"/>

<%= my_image_tag(...) %>
<img src="..."/>

<%= my_image_tag(..., { :alt => 'alt tag', :title => 'title tag' }) %>
<img src="..." alt="alt tag", title="title tag"/>
Ec5a599777854c540fd102ef4691fe10?d=identicon&s=25 Rimantas Liubertas (Guest)
on 2007-03-02 16:31
(Received via mailing list)
> Short version: As far as tooltips go, :alt = IE and :title = FF. You'll need
> both to display a tooltip across browsers.
<...>

No. IE will display alt attribute as tooltip, Firefox won't (and
sholudn't). However,
they will both display title attribute.
Alt is supposed to be used when image cannot be shown - not available
on server, user
has disabled images, text-only browser.


Regards,
Rimantas
--
http://rimantas.com/
70225136eacd3d870f64e03bff678655?d=identicon&s=25 Russell Norris (Guest)
on 2007-03-03 15:09
(Received via mailing list)
Heh. You caught me. I didn't mention that IE respects :title [because I
didn't think it necessary since I was talking about specifically about
tooltips alt vs. title ]. Please don't embarrass me further by pointing
out
that other browsers than IE and FF support these tags as well. ;)

RSL
D39d7c8071ed8bed6b851c64697b1a29?d=identicon&s=25 Oliver Paulus (paoli)
on 2007-03-05 17:43
(Received via mailing list)
I thought there is a ruby way of doing this. Now I implemented a small
plugin which extends ActionView::Base, ActionView::Helpers::TagHelper
tag method to have a "single point of change" (DRY) - because I needed
it for image_tag, image_submit_tag, etc.
Thank you for your help.
5498d1507784752a878d7cf33be13f6a?d=identicon&s=25 Scott Mathieson (uberkorp)
on 2007-03-05 21:38
(Received via mailing list)
Further to Rimantas explanation, browser implementation isn't really
the issue here as the two attributes are intended for different
purposes and setting them to the same value is incorrect; you can find
a good outline of the issues @
http://www.456bereastreet.com/archive/200412/the_a...
This topic is locked and can not be replied to.