“Highlighting Text in Rails “
January 29, 2009
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:
view plain copy to clipboard print
1.
With this bit of CSS:
view plaincopy to clipboardprint
1. .highlight { background:#ff0; }
.highlight { background:#ff0; }
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:
view plaincopy to clipboardprint
1.
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:
view plaincopy to clipboardprint
1. ‘\1‘) %>
‘\1‘) %>
This ability to insert arbitrary surrounding markup makes highlight more flexible, if you let yourself think out of the box:
view plaincopy to clipboardprint
1. ‘\1) %>
‘\1) %>
It’s worth taking a dip into ActionView::Helpers occasionally to see what other bits of functionality are lurking that you’ve forgotten about.
Source : http://afreshcup.com/2009/01/29/highlighting-text-in-rails/
Entry Filed under: ruby on rails. .
Trackback this post | Subscribe to the comments via RSS Feed