Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed the truncate filter to handle lengths shorter than the "..." co…

…ntinuation string being appended

git-svn-id: svn://svn.tt2.org/tt/Template2/trunk@1242 d5a88997-0a34-4036-9ed2-92fb5d660d91
  • Loading branch information...
commit 8e3b1efeedbf2cdcd827b3f755fb3d30f83c7c5c 1 parent 677ffb7
Andy Wardley authored
Showing with 31 additions and 6 deletions.
  1. +12 −0 Changes
  2. +16 −5 lib/Template/Filters.pm
  3. +3 −1 t/filter.t
12 Changes
View
@@ -11,6 +11,18 @@
#========================================================================
#-----------------------------------------------------------------------
+# Version 2.21_02 - 4th July 2009
+#------------------------------------------------------------------------
+
+* Added UTF8 support to the XS Stash.
+ https://rt.cpan.org/Ticket/Display.html?id=45842
+
+* Fixed the truncate filter to handle lengths shorter than the '...'
+ string being appended on the end.
+ https://rt.cpan.org/Ticket/Display.html?id=45617
+
+
+#-----------------------------------------------------------------------
# Version 2.21_01 - 4th July 2009
#------------------------------------------------------------------------
21 lib/Template/Filters.pm
View
@@ -26,8 +26,10 @@ use base 'Template::Base';
use Template::Constants;
use Scalar::Util 'blessed';
-our $VERSION = 2.86;
-our $AVAILABLE = { };
+our $VERSION = 2.87;
+our $AVAILABLE = { };
+our $TRUNCATE_LENGTH = 32;
+our $TRUNCATE_ADDON = '...';
#------------------------------------------------------------------------
@@ -531,13 +533,22 @@ sub remove_filter_factory {
sub truncate_filter_factory {
my ($context, $len, $char) = @_;
- $len = 32 unless defined $len;
- $char = "..." unless defined $char;
+ $len = $TRUNCATE_LENGTH unless defined $len;
+ $char = $TRUNCATE_ADDON unless defined $char;
+
+ # Length of char is the minimum length
+ my $lchar = length $char;
+ if ($len < $lchar) {
+ $char = substr($char, 0, $len);
+ $lchar = $len;
+ }
return sub {
my $text = shift;
return $text if length $text <= $len;
- return substr($text, 0, $len - length($char)) . $char;
+ return substr($text, 0, $len - $lchar) . $char;
+
+
}
}
4 t/filter.t
View
@@ -479,11 +479,13 @@ The cat sat on the mat
Mary ...
-- test --
+[% 'Hello World' | truncate(2) +%]
[% 'Hello World' | truncate(8) +%]
[% 'Hello World' | truncate(10) +%]
-[% 'Hello World' | truncate(20) +%]
[% 'Hello World' | truncate(11) +%]
+[% 'Hello World' | truncate(20) +%]
-- expect --
+..
Hello...
Hello W...
Hello World
Please sign in to comment.
Something went wrong with that request. Please try again.