A Fresh Cup is Mike Gunderloy's software development weblog, covering Ruby on Rails and whatever else I find interesting in the universe of software. I'm a full-time Rails developer and contributor, available for long- or short-term consulting, with solid experience in working as part of a distributed team. If you'd like to hire me, drop me a line. I'm also the author of Rails Rescue Handbook and Rails Freelancing Handbook.

Navigation
« Rails 2.3: Localized Views | Main | Double Shot #381 »
Thursday
29Jan2009

Highlighting Text in Rails

Presumably you've seen those Web 2.0 sites that highlight random words in the marketing text to draw your attention. A little-known view helper in Rails makes this sort of thing trivial. For example, you can combine this markup in your view:

[sourcecode language='ruby']
<%= highlight("Rails is a fabulous framework.
When you start 'Riding the Rails' you'll never go back.",
"Rails") %>
[/sourcecode]

With this bit of CSS:

[sourcecode language='css']
.highlight { background:#ff0; }
[/sourcecode]

And you get this result:

highlight

Easy enough. But wait, there's more! You can specify an entire array of words and phrases to highlight:

[sourcecode language='ruby']
<%= highlight(@spam.body, ['free', 'money', 'claim']) %>
[/sourcecode]

Even better, highlight supports a :highlight option, which lets you specify a custom string to use for highlighting. The token \1 will be replaced with the text to be highlighted. This lets you change the HTML markup:

[sourcecode language='ruby']
<%= highlight(@marketing_text, 'today',
:highlighter => '\1') %>
[/sourcecode]

This ability to insert arbitrary surrounding markup makes highlight more flexible, if you let yourself think out of the box:

[sourcecode language='ruby']
<%= highlight(@memberlist, @users.map(&:name),
:highlighter => '\1) %>
[/sourcecode]

It's worth taking a dip into ActionView::Helpers occasionally to see what other bits of functionality are lurking that you've forgotten about.

Reader Comments (5)

Thanks for this, always nice to get tips on unknown gems inside Rails.

January 29, 2009 | Unregistered CommenterAndrew Timberlake

I like that last tip. It's a bit inefficient but could be useful given smaller amounts of data.

It's also good to check out where Rails extends the Ruby core classes. Just today I found Hash#diff which made comparing two ActiveRecord objects a no brainer.

January 29, 2009 | Unregistered CommenterEric Davis

"Even better, highlight supports a :highlight option"

I think that should be "a :highlighter option".

Good article!

February 1, 2009 | Unregistered CommenterJeffMo

Just wanted to point out that a recent commit (http://github.com/rails/rails/commit/91eeb0ff119d34d0fcdb44d3d7fcbb7924208e05) applied a patch from my coworker Dan Weinand that makes highlight work much better against arbitrary HTML.

I know that I dismissed #highlight after realizing that it did not play well with HTML. This commit makes it much more useful.

Thanks for reminding us about #highlight

February 4, 2009 | Unregistered CommenterTom Brice

Your site is awesome very useful for starters of ror like me. i would really appreciate if u could release a pdf of all your work on rails.

Thanks

February 10, 2009 | Unregistered Commenterjo

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>