“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. .

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Recent Posts

Tags

Active Record australian gp calendar date select capistrano capitalize debugging Debugging in rails edge rails formula 1 For those who dont want to update the prototype.js fun Fun baby fun google html in join javascript linus vs windows compatibility linux logging love song malaysian gp 2008 results melbourne migrations music mysql plugin poetry rails regexp regular expression results formula 1 opening race romantic poetry ror Ruby ruby on rails safe html sms song SQL string case handler for strings syantax syntax error for migrations on linux Text Area they can checkout 1.8.3 of the calender date select

Blogroll

Pages