Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trim text when showing a article summary?

I am showing a list of articles, and want to cut the text after 200 characters, how can I do this?

like image 820
Blankman Avatar asked Dec 18 '10 03:12

Blankman


1 Answers

Is this what your a looking for?

http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-truncate

And so you might have something in your view like:

<div>
<% text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce et nunc justo, a pharetra tortor. Morbi auctor arcu dapibus velit tincidunt in posuere nunc imperdiet. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Cras venenatis libero vitae eros cursus consequat. In hac habitasse platea dictumst. Vestibulum tempor nibh et neque eleifend bibendum. Vivamus felis sapien, rutrum a scelerisque vel, lacinia eget diam. Nunc dapibus posuere turpis, quis posuere neque sollicitudin vitae. Curabitur ullamcorper faucibus tellus, a tristique elit facilisis ut. Etiam rhoncus molestie sollicitudin. Integer nulla elit, vehicula sit amet commodo nec, gravida eu eros. Vivamus pulvinar accumsan tortor, eu rhoncus nisl interdum a. Morbi ut ultrices sapien. Donec eleifend fermentum pretium. Pellentesque ipsum urna, convallis at pulvinar adipiscing, pulvinar sit amet nisi. Maecenas vel tincidunt dolor."%>
<%=
  truncate(text, :length => 200,  :separator => ' ') do
    link_to "see more..."
  end
%>
</div>
like image 142
aarona Avatar answered Oct 18 '22 20:10

aarona