“Highlighting Text in Rails “

January 29, 2009 at 6:56 pm Leave a comment

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. Tags: .

Installing ActiveMQ “Dynamic finders with hash attributes for creation”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Recent Posts

Pages


Follow

Get every new post delivered to your Inbox.