* apv <apv at sedition.com> [2005/12/20 00:07]:
> Just to put this out there. I'd love it if truncate took an optional
> second argument. Dot-dot-dot is fine but for a utf8 website I'd rather
> use a literal ellipsis and even for iso-8859-1, I prefer the entities,
> … and …. I can also think of times I'd like to use an
> en-dash or nothing at all for truncating.
This is not a bad idea, but:
> return sub {
> my $text = shift;
> return $text if length $text < $len;
> - return substr($text, 0, $len - 3) . "...";
> + return substr($text, 0, $len - length($char)) . $char;
In the case of something like …, this will truncate eight
characters from $text, but replace it with something much smaller (it
looks like two characters width in my browser).
Maybe something that accepts an optional length, in the case of
providing your own truncate string:
sub truncate_filter_factory {
my ($context, $len, $char, $chop) = @_;
$len = 32 unless defined $len;
if ($chop) {
$len -= int($chop);
}
else {
$char = "..." unless defined $char;
}
return sub {
my $text = shift;
return $text if length $text < $len;
return substr($text, 0, $len) . $char;
}
}
The normal case is not changed, and in the case of providing your own
characters, you can specify how much to truncate (default none):
[% text | truncate(40, '…', 3) %]
*shrug* Maybe this is too much, though.
(darren)
--
Remember, any design flaw you're sufficiently snide about becomes a
feature.
-- Dan Sugalski
_______________________________________________
templates mailing list
[email protected]
http://lists.template-toolkit.org/mailman/listinfo/templates