[Templates] proposed truncate filter change

apv apv@sedition.com
Tue, 20 Dec 2005 00:07:19 -0800


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.

Thanks for looking!

-Ashley

Possible patch:

--- /usr/local/lib/perl5/site_perl/5.8.2/darwin/Template/Filters.pm      
Mon Oct  4 03:27:39 2004
+++ ./Filters.pm        Tue Dec 20 00:03:46 2005
@@ -481,13 +481,13 @@
   
#----------------------------------------------------------------------- 
-

  sub truncate_filter_factory {
-    my ($context, $len) = @_;
+    my ($context, $len, $char) = @_;
      $len = 32 unless defined $len;
-
+    $char = "..." unless defined $char;
      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;
      }
  }