Can anyone think of an easier way to do this?
How about using the 'highlight' helper method in your view instead?
Can anyone think of an easier way to do this?
How about using the 'highlight' helper method in your view instead?
I was just looking into Text::HTMLHighlighter....is that what you mean?
Aaron Chase wrote:
I was thinking about the one in ActionView::Helpers::TextHelper. The description is as follows:
highlight(text, phrases, highlighter = '<strong class="highlight">\1</
')
Highlights one or more phrases everywhere in text by inserting it into a highlighter string. The highlighter can be specialized by passing highlighter as a single-quoted string with \1 where the phrase is to be inserted (defaults to '<strong class="highlight">\1</strong>')
hmmmm....that looks like it might work...thanks
How do I include that Rails application. I tried just putting this in my view and it does nothing:
highlight('this is sample text', 'text')
How does rail know about it..maybe I should requiring action_view ?
Aaron Chase wrote:
It should be available in your view. I just tried adding this to my view and it worked as advertised:
<%= highlight("this is sample text", "text") %>
I'm using Rails 2.0.2.
sorry for that...I didn't even try it with the <%= at the beginning.....it does work for me too now....thanks for the help on this.
The only issue I have is that it makes the text strong bold....what I need is to make it highlighted in red or other colors....I don't see anything in the doco to do that.
I installed Text::HTMLHighlighter and all the tests ran successfully but I can't seem to figure out how to get it to work in my view. There is an .rb file that I'm supposed to call somehow and I can't figure you how to do it. The directions seem to make me think Text::HTMLHighlighter is made to work with other ruby files and not specifically for Rails...not sure...
jackster
Aaron Chase wrote:
As stated in the docs, you can specialize the highlighter by providing a value for the 3rd argument. The default is to highlight using <strong> text, but you can get the effect you want by doing something like:
<%= highlight('this is sample text', 'text', '<span class='highlight"> \1</span>') %>
and adding this to your CSS file:
.highlight {color: red}
Burning the candle at both ends, I see from the timestamp of your last message
You've helped me alot, Aaron...and thank you for that.
As you have pointed out...defining highlight in my CSS works perfectly. In fact, I found that as soon as I defined it, the following statement works:
<%= highlight('this is simple text', 'text') %>
And I didn't have to add the <span class = "highlight"> part although that worked as well.
The last question I have is what happens when I want to highlight more than one word if it appears in my variable?
For instance, I have a variable that contains a date, what I need to be able to do is highlight Thu when it's Thursday but always highlight 2008. This works to always highlight 2008:
<%= highlight(trap.datetime.to_s, '2008') %>
I was hoping I could do something like this but it doesn't work because it looks for both of them in a string:
<%= highlight(trap.datetime.to_s, 'Thu, 2008') %>
What I'm actually trying to accomplish here, is to highlight certain words in my network traps in red. For instance, if I got a trap that contained the word "BGP", I want that highlighted in red as well as if the word "Spanning-tree" is contained in the trap. This is why I was hoping to be able to specify several words to be highlighted if they exist in my variable.
thanks alot Aaron,
jackster
Aaron Chase wrote:
Specifying an array for the 2nd argument should do the trick:
<%= highlight(trap.datetime.to_s, ['Thu', '2008']) %>
You may have to add spaces to your phrase strings to prevent the highlighter from highlighting things you didn't intend. For example, I tried this
<%= highlight('foo bar foobar', ['foo', 'bar']) %>
and it highlighted the 'foobar' substring since 'foo' matched the first part of the string and 'bar' matched the last part of it. Changing the phrase array to ['foo ', 'bar '] (note the embedded whitespace) fixed it.
I can't thank you enough for your help on this...I'm up and running on this now and all is looking good.
jackster
Aaron Chase wrote:
Aaron....I wanted to run one more thing by you on this subject if you don't mind?
I am using the following command which works nicely to highlight key words in red if they are contained in my network trap:
<%= highlight(trap.desc4.to_s,['DOWN', 'COLD', 'BGP', 'RESTART', 'SPAN']) %>
Can you think of an easy way to highlight some words in red and others in another color?
I have never used a conditional if statement in a view before and don't even know if that's possible....if it was, I was thinking something like this might be the a solution:
if <%= highlight(trap.desc4.to_s,['DOWN', 'COLD', 'BGP', 'RESTART', 'SPAN']) %> else <%= highlight_blue(trap.desc4.to_s,['UP', 'HOT', 'OSPF', 'START', 'LINK']) %>
...or perhaps you might know of an easier solution which I am overlooking?
thanks for all the help
jackster
jackster the jackle wrote:
Just off the top of my head, you could write your own helper something like this:
def highlight_with_color(text, phrases, color_spec) highlight(text, phrases, '<span style="color:#{color_spec};">\1</
')
end
and then use it in your view like this:
<%= highlight_with_color("foo bar", ["foo, bar"], "#abcdef") %>
Disclaimer: I haven't actually tried this code, but I think something like this should work. Someone in this group may also have a better suggestion...