# Truncate text?

**URL:** <https://www.ruby-forum.com/t/truncate-text/138696>\
**Category:** Radiant CMS\
**Created:** [May 26, 2008, 10:42pm UTC](https://www.ruby-forum.com/t/truncate-text/138696 "2008-05-26T22:42:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![scottrutherford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d07c76/32.png) [@scottrutherford](https://www.ruby-forum.com/u/scottrutherford)\
**Post date:** [May 26, 2008, 10:42pm UTC](https://www.ruby-forum.com/t/truncate-text/138696/1 "2008-05-26T22:42:02Z")

</div>

Is there a Truncate tag or function? I’d like to show just a snippet of  
posts on my homepage without having to do so manually.

So, that we enter a post, it can be any length, but on our homepage, we  
only show, say 150 characters with a …(more) link following it (like  
the Truncate text helper in rails).

---

<div class="post-metadata">

**Author:** ![scottrutherford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d07c76/32.png) [@scottrutherford](https://www.ruby-forum.com/u/scottrutherford)\
**Post date:** [May 26, 2008, 11:15pm UTC](https://www.ruby-forum.com/t/truncate-text/138696/2 "2008-05-26T23:15:46Z")

</div>

Scott,

No, there’s not a default one, but it’s easy to implement:

tag ‘truncate’ do |tag|  
length = tag.attr[‘length’].to\_i rescue 150  
tag.expand[0…(length-1)] + “…”  
end

Obviously you could do clever things like massaging HTML, etc in  
addition to what’s there, but there’s a start.

Sean

---

<div class="post-metadata">

**Author:** ![scottrutherford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d07c76/32.png) [@scottrutherford](https://www.ruby-forum.com/u/scottrutherford)\
**Post date:** [May 27, 2008, 3:08am UTC](https://www.ruby-forum.com/t/truncate-text/138696/3 "2008-05-27T03:08:11Z")

</div>

I’m a relative newbie to Radiant. Is this code something that can be  
put directly into a page or snippet? Or does this need to go in an  
external class and called from a page/snippet?

Scott

Sean C. wrote:

> Scott,
> 
> No, there’s not a default one, but it’s easy to implement:
> 
> tag ‘truncate’ do |tag|  
> length = tag.attr[‘length’].to\_i rescue 150  
> tag.expand[0…(length-1)] + “…”  
> end
> 
> Obviously you could do clever things like massaging HTML, etc in  
> addition to what’s there, but there’s a start.
> 
> Sean

---

<div class="post-metadata">

**Author:** ![scottrutherford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d07c76/32.png) [@scottrutherford](https://www.ruby-forum.com/u/scottrutherford)\
**Post date:** [May 27, 2008, 3:44am UTC](https://www.ruby-forum.com/t/truncate-text/138696/4 "2008-05-27T03:44:37Z")

</div>

That code is a radius tag. You can read more about them here:

[http://radius.rubyforge.org/files/QUICKSTART.html](http://radius.rubyforge.org/files/QUICKSTART.html)

Additionally, you would pack this tag definition into your own  
extension. You can read more about creating extensions here:

[http://wiki.radiantcms.org/Creating\_Radiant\_Extensions](http://wiki.radiantcms.org/Creating_Radiant_Extensions)

Good luck.

Cheers,  
Marty

---

<div class="post-metadata">

**Author:** ![scottrutherford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d07c76/32.png) [@scottrutherford](https://www.ruby-forum.com/u/scottrutherford)\
**Post date:** [May 27, 2008, 12:24pm UTC](https://www.ruby-forum.com/t/truncate-text/138696/5 "2008-05-27T12:24:13Z")

</div>

Scott,

The search extension comes with a somewhat better worked out truncate  
tag;  
Besides truncating, it first strips all html tags. Without this, the  
truncate function may return a string that holds i.e. an un-closed

# tag, which would screw up your page. Here’s how it works;

\<r:truncate\_and\_strip [length=“100”] /\>  
Truncates and strips all HTML tags from the content of the contained  
block.  
Useful for displaying a snippet of a found page. The optional  
`length’ attribute  
specifies how many characters to truncate to.

If you don’t mind the overhead, just install the search extension from  
here:

[http://svn.radiantcms.org/radiant/trunk/extensions/search/](http://svn.radiantcms.org/radiant/trunk/extensions/search/)

Or, create your own extension, and copy the radius tag to it;

desc %{ \<r:truncate\_and\_strip [length=“100”] /\>  
Truncates and strips all HTML tags from the content of the contained  
block.  
Useful for displaying a snippet of a found page. The optional  
`length’ attribute  
specifies how many characters to truncate to.  
}

tag ‘truncate\_and\_strip’ do |tag|  
tag.attr[‘length’] ||= 100  
length = tag.attr[‘length’].to\_i  
helper = ActionView::Base.new  
helper.truncate(helper.strip\_tags(tag.expand).gsub(/\s+/," "),  
length)  
end

---

<div class="post-metadata">

**Author:** ![scottrutherford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d07c76/32.png) [@scottrutherford](https://www.ruby-forum.com/u/scottrutherford)\
**Post date:** [May 27, 2008, 4:36pm UTC](https://www.ruby-forum.com/t/truncate-text/138696/6 "2008-05-27T16:36:20Z")

</div>

On Tue, 2008-05-27 at 12:24 +0200, Benny D. wrote:

> \<r:truncate\_and\_strip [length=“100”] /\>

I was also in need of something like this. Thank you very much.

~Nate
