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).
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
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
That code is a radius tag. You can read more about them here:
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
Good luck.
Cheers,
Marty
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/
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
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