Thursday
29Jan2009
Highlighting Text in Rails
Thursday, January 29, 2009 at 2:06AM
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:

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,
[sourcecode language='ruby']
<%= highlight(@marketing_text, 'today',
:highlighter => '\1') %>
[/sourcecode]
This ability to insert arbitrary surrounding markup makes
[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.
[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:

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.
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.
"Even better, highlight supports a :highlight option"
I think that should be "a :highlighter option".
Good article!
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
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