- 擷取前N個字元 - truncate(text, options = {}, &block)
Definition-
Truncates a giventextafter a given:lengthiftextis longer than:length(defaults to 30). The last characters will be replaced with the:omission(defaults to “…”) for a total length not exceeding:length.-
Pass a
:separatorto truncatetextat a natural break. - Pass a block if you want to show extra content when the text is truncated.
-
The result is marked as HTML-safe, but it is escaped by default, unless
:escapeisfalse. Care should be taken iftextcontains HTML tags or entities, because truncation may produce invalid HTML (such as unbalanced or incomplete tags).
truncate("Once upon a time in a world far far away")
# => "Once upon a time in a world..."
truncate("Once upon a time in a world far far away", length: 17)
# => "Once upon a ti..."
truncate("Once upon a time in a world far far away", length: 17, separator: ' ')
# => "Once upon a..." truncate("And they found that many people were sleeping better.", length: 25, omission: '... (continued)')
# => "And they f... (continued)"
truncate("<p>Once upon a time in a world far far away</p>")
# => "<p>Once upon a time in a wo..."
Ref: http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-truncate -
Pass a
2013年12月18日 星期三
[程式] Rails Tips
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言