Skip to content

Commit 47b2f68

Browse files
Merge pull request rails#50178 from akhilgkrishnan/improve-documentation-for-truncate
Improve documentation for truncate text helper [skip ci]
2 parents 3f42616 + c5add29 commit 47b2f68

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

actionview/lib/action_view/helpers/text_helper.rb

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,36 @@ def safe_concat(string)
6868
output_buffer.respond_to?(:safe_concat) ? output_buffer.safe_concat(string) : concat(string)
6969
end
7070

71-
# Truncates a given +text+ after a given <tt>:length</tt> if +text+ is longer than <tt>:length</tt>
72-
# (defaults to 30). The last characters will be replaced with the <tt>:omission</tt> (defaults to "...")
73-
# for a total length not exceeding <tt>:length</tt>.
71+
# Truncates +text+ if it is longer than a specified +:length+. If +text+
72+
# is truncated, an omission marker will be appended to the result for a
73+
# total length not exceeding +:length+.
7474
#
75-
# Pass a <tt>:separator</tt> to truncate +text+ at a natural break.
75+
# You can also pass a block to render and append extra content after the
76+
# omission marker when +text+ is truncated. However, this content _can_
77+
# cause the total length to exceed +:length+ characters.
7678
#
77-
# Pass a block if you want to show extra content when the text is truncated.
79+
# The result will be escaped unless <tt>escape: false</tt> is specified.
80+
# In any case, the result will be marked HTML-safe. Care should be taken
81+
# if +text+ might contain HTML tags or entities, because truncation could
82+
# produce invalid HTML, such as unbalanced or incomplete tags.
7883
#
79-
# The result is marked as HTML-safe, but it is escaped by default, unless <tt>:escape</tt> is
80-
# +false+. Care should be taken if +text+ contains HTML tags or entities, because truncation
81-
# may produce invalid HTML (such as unbalanced or incomplete tags).
84+
# ==== Options
85+
#
86+
# [+:length+]
87+
# The maximum number of characters that should be returned, excluding
88+
# any extra content from the block. Defaults to 30.
89+
#
90+
# [+:omission+]
91+
# The string to append after truncating. Defaults to <tt>"..."</tt>.
92+
#
93+
# [+:separator+]
94+
# A string or regexp used to find a breaking point at which to truncate.
95+
# By default, truncation can occur at any character in +text+.
96+
#
97+
# [+:escape+]
98+
# Whether to escape the result. Defaults to true.
99+
#
100+
# ==== Examples
82101
#
83102
# truncate("Once upon a time in a world far far away")
84103
# # => "Once upon a time in a world..."

0 commit comments

Comments
 (0)