I am working on an app that has user generated content. What I’d like
to do, is display X characters of, say, a title and put “…” for the
rest. This sounds like something you see in soc networking type app
and wondering if there is some code out there to do this. thanks.
<%= h truncate( model.property, X ) %>
On Mon, Jun 9, 2008 at 5:16 AM, nahabed [email protected] wrote:
I am working on an app that has user generated content. What I’d like
to do, is display X characters of, say, a title and put “…” for the
rest. This sounds like something you see in soc networking type app
and wondering if there is some code out there to do this. thanks.
–
Nahabed,
Just in case you didn’t follow Lucas’s explanation the ‘X’ is the
length you wish to display on the form.
Kathleen
Lucas wrote:
<%= h truncate( model.property, X ) %>
Hey, I’ve just got a quick question on this (I’ve run into needing this
same thing, but I’ve done it a different way). Say I want to limit
something to 50 characters before cutting it off and adding the “…”.
If it turns out that whatever I want to limit ends up only being 30
characters, will it still add the “…”? I’d assume it wouldn’t,
because Rails is usually smarter than that, but its good to check before
I go changing my app to use this method (which is way better than how
I’ve been doing it)
Dan __ wrote:
Lucas wrote:
<%= h truncate( model.property, X ) %>
Hey, I’ve just got a quick question on this (I’ve run into needing this
same thing, but I’ve done it a different way). Say I want to limit
something to 50 characters before cutting it off and adding the “…”.
If it turns out that whatever I want to limit ends up only being 30
characters, will it still add the “…”? I’d assume it wouldn’t,
because Rails is usually smarter than that, but its good to check before
I go changing my app to use this method (which is way better than how
I’ve been doing it)
(model.property.length > X ? truncate(model.property, X-3)+’…’ :
model.property)
or something like that…
DyingToLearn wrote:
@Dan, in short, rails is smart enough. See
ActionView::Helpers::TextHelper
Beautiful! Simplifies my code a decent amount (gets rid of the if
statements I had before to do the same job). Thanks a bunch
(model.property.length > X ? truncate(model.property, X-3)+’…’ :
model.property)
Doh! Ignore that… truncate… Nice.
I was under the impression that’s what truncate did, Jeffrey.
Quoting nahabed [email protected]:
I am working on an app that has user generated content. What I’d like
to do, is display X characters of, say, a title and put “…” for the
rest. This sounds like something you see in soc networking type app
and wondering if there is some code out there to do this. thanks.
truncate a string to N characters, add elipsis if over
def short_desc(s, n=80)
if s.nil?
‘’
elsif s.length <= n
s
else
i = n - 4 # 4 characters in ellipsis
while i > 0 && " \t".include?(s[i]) # is whitespace?
i = i - 1
end
if i <= 0
s
else
s.slice(0, i) + ’ …’
end
end
end
HTH,
Jeffrey
My bad. Made an improvement without verifying the result. Corrected
line
below uncommented.
Sorry,
Jeffrey
Quoting Jeffrey L. Taylor [email protected]:
'' elsif s.length <= n s else i = n - 4 # 4 characters in ellipsis while i > 0 && " \t".include?(s[i]) # is whitespace?
while i > 0 && !" \t".include?(s[i]) # is not whitespace?
hi
I guess u want to do something like this
some text upto say 30 char and after that … so u can restrict just by
doing
<%= t.content[0…30]%>…
Thanks
Dhaval P.
Software Engineer
sales(AT)railshouse(DOT)com
On Jun 13, 2008, at 12:36 PM, Jeffrey L. Taylor wrote:
Quoting Ryan B. (Radar) [email protected]:
I was under the impression that’s what truncate did, Jeffrey.
No, truncate just chops it off at N characters, possibly in mid-
word. My code
truncates at whitespace, i.e., between words. (Or at least it does
after the
correction I just posted.)Jeffrey
You really, REALLY ought to have tests for this:
class TruncateTest < Test::Unit::TestCase
def test_fits
str = “short”
assert_equal str, truncate(str, 10)
end
def test_backs_up_to_space
str = “short filibuster”
exp = “short …”
assert_equal exp, truncate(str, 10)
end
def test_breaks_word_if_no_space
str = “antidisestablishmentarianism”
exp = “antidis…”
assert_equal exp, truncate(str, 10)
end
end
I didn’t look back at your code so these tests might not actually be
the results that you want, but this ought to give you the right idea.
-Rob
Quoting Ryan B. (Radar) [email protected]:
I was under the impression that’s what truncate did, Jeffrey.
No, truncate just chops it off at N characters, possibly in mid-word.
My code
truncates at whitespace, i.e., between words. (Or at least it does
after the
correction I just posted.)
Jeffrey
Shorter, faster version
truncate a string to N characters, add elipsis if over
def short_desc(s, n=80)
if s.nil?
‘’
elsif s.length <= n
s
else
# find last break character (space, tab, hyphen, slash,
underscore)
i = s.rindex(/[ \t-/_]/, n)
i = n - 4 if i.nil? # none, just truncate()
s.slice(0, i+1) + ‘…’
end
end
Quoting Rob B. [email protected]:
end
end
I didn’t look back at your code so these tests might not actually be
the results that you want, but this ought to give you the right idea.
It’s added to the TODO list. I’ll start with your examples.
Thanks,
Jeffrey
Quoting Jeffrey L. Taylor [email protected]:
[snip]
It’s added to the TODO list. I’ll start with your examples.
Done.