From 46b4927562b9679deec973e1b2d6dbdc7b4e755c Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Wed, 29 Aug 2012 16:05:35 +0200 Subject: [PATCH] =?utf8?q?L.truncate:=20Funktion=20zum=20intelligenten=20V?= =?utf8?q?erk=C3=BCrzen=20eines=20Strings?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- SL/Template/Plugin/L.pm | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/SL/Template/Plugin/L.pm b/SL/Template/Plugin/L.pm index 43ac5d250..8523db9e0 100644 --- a/SL/Template/Plugin/L.pm +++ b/SL/Template/Plugin/L.pm @@ -570,6 +570,18 @@ sub dump { return '
' . Data::Dumper::Dumper(@_) . '
'; } +sub truncate { + my ($self, $text, @slurp) = @_; + my %params = _hashify(@slurp); + + $params{at} ||= 50; + $params{at} = 3 if 3 > $params{at}; + $params{at} -= 3; + + return $text if length($text) < $params{at}; + return substr($text, 0, $params{at}) . '...'; +} + 1; __END__ @@ -918,6 +930,16 @@ the resulting tab will get ignored by C: L.tab('Awesome tab wih much info', '_much_info.html', if => SELF.wants_all) +=item C + +Returns the C<$text> truncated after a certain number of +characters. + +The number of characters to truncate at is determined by the parameter +C which defaults to 50. If the text is longer than C<$params{at}> +then it will be truncated and postfixed with '...'. Otherwise it will +be returned unmodified. + =back =head1 MODULE AUTHORS -- 2.20.1