开发者

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开发者_Go百科, how can I do this?


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜