Reputation: 197
I am using this code to display the headline if there is one
<%= @user.headline if @user.headline? %>
I have it limited to 100 characters. How can I break this headline after 50 characters and display the broken part below? I am planning to have them centered in two lines.
For example:
If this is the headline which goes over 100 characters then I want to display it like this
Display:
If this is the headline which goes over 100 characters then
I want to display it like this
Upvotes: 1
Views: 1786
Reputation: 10147
You can break string as follows:
head_line = @user.headline.scan(/.{1,50}/)
In your case you can use word_wrap
:
word_wrap(@user.headline, :line_width => 50)
Upvotes: 0
Reputation: 5388
You could use word_wrap from text helper:
include ActionView::Helpers::TextHelper
# or in a controller
# helper :text
word_wrap(@user.headline, :line_width => 50)
Upvotes: 3
Reputation:
The word_wrap
method was made specifically to address this.
However, it inserts newlines instead of what you'd need for breaking lines in HTML (such as encasing different lines in their own <p>
tags). I'd implement that functionality as a helper method after taking a good look at how the TextHelper's word_wrap
method is implemented.
Upvotes: 3