############################################################################### #Filter.pm #Last Change: 2009-01-19 #Copyright (c) 2009 Marc-Seabstian "Maluku" Lucksch #Version 0.8 #################### #This file is part of the Dotiac::DTL project. #http://search.cpan.org/perldoc?Dotiac::DTL # #Filter.pm is published under the terms of the MIT license, which basically #means "Do with it whatever you want". For more information, see the #license.txt file that should be enclosed with libsofu distributions. A copy of #the license is (at the time of writing) also available at #http://www.opensource.org/licenses/mit-license.php . ############################################################################### package Dotiac::DTL::Filter; use strict; use warnings; require Scalar::Util; our $VERSION = 0.8; sub add { my $value=shift; my $add=shift; $value->set($value->repr+$add->repr) if $value->number and $add->number; $value->set($value->repr.$add->repr) unless $value->number and $add->number; return $value; } sub addslashes { my $value =shift; my $val=$value->repr(); $val=~s/([\\'"])/\\$1/g; $value->set($val); return $value; } sub capfirst { my $value=shift; return $value->set(ucfirst $value->repr); } sub center { my $value=shift; my $length=shift; return $value unless $length->number; my $padding = shift; my $pad=" "; $pad=substr($padding->repr,0,1) if $padding; my $val=$value->repr; my $len=$length->repr; $len-=CORE::length $val; $val=($pad x int($len/2)).$val.($pad x int($len/2)).($len%2?$pad:""); $value->set($val); return $value; } sub cut { my $value=shift; my $val=$value->repr(); my $t=shift; $t=$t->repr(); $val=~s/\Q$t//g; $value->set($val); return $value; } #locale stuff our @datemonths=qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ); our @datemonthl=qw( January February March April May Juni Juli August September October November December ); our @datemontha=qw( Jan. Feb. March April May Juni Juli Aug. Sep. Oct. Nov. Dec. ); our @weekdays=qw/Sun Mon Tue Wed Thu Fri Sat/; our @weekdayl=qw/Sunday Monday Tuesday Wednesday Thursday Friday Saturday/; our @timeampm=qw/a.m. p.m. AM PM/; our @timespotnames=qw/midnight noon/; our @datesuffixes=qw/th st nd rd/; #qw/Default day1 day2 day3 day4 day5... sub date { my $value=shift; return $value unless $value->number() or $value->array(); my $time=$value->repr(); my $safe=0; my $string=shift; if (not defined $string or not $string->scalar()) { $string=$Dotiac::DTL::DATE_FORMAT; $safe=1; } else { $safe=$string->safe(); $string=$string->repr; } my @t; if ($value->number()) { @t=localtime($time); } else { @t=@{$value->content}; } my @s=split //,$string; my $res; while (my $s=shift(@s)) { if ($s eq '\\') { $res.=shift(@s); } elsif ($s eq "a") { if ($t[2] > 12 or ($t[2] == 12 and $t[1] > 0)) { $res.=$timeampm[0]; } else { $res.=$timeampm[1]; } } elsif ($s eq "A") { if ($t[2] > 12 or ($t[2] == 12 and $t[1] > 0)) { $res.=$timeampm[2]; } else { $res.=$timeampm[3]; } } elsif ($s eq "b") { $res.=lc($datemonths[$t[4]]); } elsif ($s eq "d") { $res.=sprintf("%02d",$t[3]); } elsif ($s eq "D") { $res.=$weekdays[$t[6]]; } elsif ($s eq "f") { my $h=$t[2]; $h=$h%12; $h=12 unless $h; $res.=$h; $res.=sprintf(":%02d",$t[1]) if ($t[1]); } elsif ($s eq "F") { $res.=$datemonthl[$t[4]]; } elsif ($s eq "g") { my $h=$t[2]; $h=$h%12; $h=12 unless $h; $res.=$h; } elsif ($s eq "G") { $res.=$t[2]; } elsif ($s eq "h") { my $h=$t[2]; $h=$h%12; $h=12 unless $h; $res.=sprintf("%02d",$h); } elsif ($s eq "H") { $res.=sprintf("%02d",$t[2]); } elsif ($s eq "i") { $res.=sprintf("%02d",$t[1]); } elsif ($s eq "j") { $res.=$t[3]; } elsif ($s eq "l") { $res.=$weekdayl[$t[6]]; } elsif ($s eq "L") { my $d=$t[5]+1900; $res.=(((not $d%4 and $d%100) or not $d%400)?"1":"0"); } elsif ($s eq "m") { $res.=sprintf("%02d",$t[4]+1); } elsif ($s eq "M") { $res.=$datemonths[$t[4]]; } elsif ($s eq "n") { $res.=$t[4]+1; } elsif ($s eq "N") { $res.=$datemontha[$t[4]]; } elsif ($s eq "O") { my @tt=localtime(0); $tt[2]+=1 if $t[8]; $res.=sprintf("%+05d",$tt[2]*100+$tt[1]); } elsif ($s eq "P") { if ($t[2] == 12 and $t[1] == 0) { $res.=$timespotnames[1]; } elsif ($t[2] == 0 and $t[1] == 0) { $res.=$timespotnames[0]; } else { my $h=$t[2]; $h=$h%12; $h=12 unless $h; $res.=$h; $res.=sprintf(":%02d",$t[1]) if ($t[1]); if ($t[2] > 12 or ($t[2] == 12 and $t[1] > 0)) { $res.=" ".$timeampm[0]; } else { $res.=" ".$timeampm[1]; } } } elsif ($s eq "r") { $res.=$weekdays[$t[6]]; $res.=", "; $res.=$t[4]+1; $res.=" ".$datemonths[$t[4]]." ".($t[5]+1900); $res.=sprintf(" %02d:%02d:%02d",$t[2],$t[1],$t[0]); my @tt=localtime(0); $tt[2]+=1 if $t[8]; $res.=sprintf(" %+05d",$tt[2]*100+$tt[1]); } elsif ($s eq "s") { $res.=sprintf("%02d",$t[0]); } elsif ($s eq "S") { if ($datesuffixes[$t[3]]) { $res.=$datesuffixes[$t[3]]; } else { $res.=$datesuffixes[0] } } elsif ($s eq "t") { if ($t[4] == 1 or $t[4]==3 or $t[4] == 5 or $t[4] == 7 or $t[4] == 8 or $t[4] == 10 or $t[4] == 12) { $res.="31"; } elsif ($t[4] == 2) { my $d=$t[5]+1900; if ((not $d%4 and $d%100) or not $d%400) { $res.="29"; } else { $res.="28"; } } else { $res.="30"; } } elsif ($s eq "T") { require POSIX; $res.=POSIX::strftime("%Z", @t); } elsif ($s eq "t") { $res.=$t[6]; } elsif ($s eq "W") { require POSIX; $res.=POSIX::strftime("%W", @t); } elsif ($s eq "y") { $res.=sprintf("%02d",($t[5]%100)); } elsif ($s eq "Y") { $res.=sprintf("%04d",$t[5]+1900); } elsif ($s eq "z") { $res.=$t[7]; } elsif ($s eq "Z") { my @tt=localtime(0); $tt[2]+=1 if $t[8]; $res.=$tt[2]*3600+$t[1]*60+$t[0]; } elsif ($s eq "\n") { $res.="n"; } elsif ($s eq "\t") { $res.="t"; } elsif ($s eq "\f") { $res.="f"; } elsif ($s eq "\b") { $res.="b"; } elsif ($s eq "\r") { $res.="r"; } else { $res.=$s; } } return Dotiac::DTL::Value->new($res,$safe); } sub default { my $val=shift; my $def=shift; return $def unless $val->true; return $val; } sub default_if_none { my $val=shift; my $def=shift; return $def unless $val->defined; return $val; } sub dictsort { my $value=shift; return $value unless $value->array(); my $by=shift; unless ($by) { $value->set([sort { if (Scalar::Util::looks_like_number($a) and Scalar::Util::looks_like_number($b)) { $a <=> $b } else { $a cmp $b } } @{$value->content}]); return $value; } $by=$by->repr(); $value->set([sort { my $aa = $a; if (ref $a) { $aa = $a->{$by} if Scalar::Util::reftype($a) eq "HASH" and exists $a->{$by}; $aa = $a->[$by] if Scalar::Util::reftype($a) eq "ARRAY" and Scalar::Util::looks_like_number($by) and exists $a->[$by]; $aa = $a->$by() if Scalar::Util::blessed($a) and $a->can($by); } my $bb = $b; if (ref $b) { $bb = $b->{$by} if Scalar::Util::reftype($b) eq "HASH" and $b->{$by}; $bb = $b->[$by] if Scalar::Util::reftype($a) eq "ARRAY" and Scalar::Util::looks_like_number($by) and exists $b->[$by]; $bb = $b->$by() if Scalar::Util::blessed($b) and $b->can($by); } if (Scalar::Util::looks_like_number($aa) and Scalar::Util::looks_like_number($bb)) { $aa <=> $bb } else { $aa cmp $bb } } @{$value->content}]); return $value; } sub dictsortreversed { my $value=shift; return $value unless $value->array(); my $by=shift; unless ($by) { $value->set([reverse sort { if (Scalar::Util::looks_like_number($a) and Scalar::Util::looks_like_number($b)) { $a <=> $b } else { $a cmp $b } } @{$value->content}]); return $value; } $by=$by->repr(); $value->set([reverse sort { my $aa = $a; if (ref $a) { $aa = $a->{$by} if Scalar::Util::reftype($a) eq "HASH" and exists $a->{$by}; $aa = $a->[$by] if Scalar::Util::reftype($a) eq "ARRAY" and Scalar::Util::looks_like_number($by) and exists $a->[$by]; $aa = $a->$by() if Scalar::Util::blessed($a) and $a->can($by); } my $bb = $b; if (ref $b) { $bb = $b->{$by} if Scalar::Util::reftype($b) eq "HASH" and $b->{$by}; $bb = $b->[$by] if Scalar::Util::reftype($a) eq "ARRAY" and Scalar::Util::looks_like_number($by) and exists $b->[$by]; $bb = $b->$by() if Scalar::Util::blessed($b) and $b->can($by); } if (Scalar::Util::looks_like_number($aa) and Scalar::Util::looks_like_number($bb)) { $aa <=> $bb } else { $aa cmp $bb } } @{$value->content}]); return $value; } sub divisibleby { my $value=shift; return Dotiac::DTL::Value->safe(0) unless $value->number; my $by=shift; return Dotiac::DTL::Value->safe(0) unless $by; return Dotiac::DTL::Value->safe(0) unless $by->number; my $res=!($value->content % $by->content); return Dotiac::DTL::Value->safe($res); } sub escape { my $value=shift; $value->escape(1); return $value; } #Not for JSON output of objects, I need to write an JSON-Addon for that. my %jsescape = ( "\n" => "\\n", "\r" => "\\r", "\t" => "\\t", "\f" => "\\f", "\b" => "\\b", '"' => "\\\"", "\\" => "\\\\", "'" => "\\'", ); sub escapejs { my $value=shift; my $val=$value->repr(); $val =~ s/([\n\r\t\f\b"'\\])/$jsescape{$1}/eg; #$val =~ s/([\x00-\x08\x0b\x0e-\x1f\x7f-\x{FFFF}])/'\\u' .sprintf("%04x",ord($1))/eg; #Won't work in Perl 5.6.0 $val =~ s/([^\x09\x0a\x0c\x0d\x20-\x7e])/'\\u' .sprintf("%04x",ord($1))/eg; $value->set($val); return $value; } #Locale crap our @filesizeformat=qw/bytes Kb Mb Gb Tb Eb Pb manybytes manybytes manybytes manybytes/; our $floatformatlocale=""; #sub { # my $v=shift; # $v=s/\./,/g; # return $v; #} sub filesizeformat { my $val=shift; return $val unless $val->number(); my $value=$val->content(); my $i=0; while ($value >= 1024.0) { $value=$value/1024.0; $i++; } if ($value < 10) { $value=sprintf("%1.2f",$value); } else { $value=sprintf("%4.1f",$value); } $value=~s/0+$//g; $value=~s/\.$//g; $value=$floatformatlocale->($value) if $floatformatlocale; $val->set($value." ".$filesizeformat[$i]); return $val; } sub first { my $value=shift; if ($value->object) { if ($value->content->can("__getitem__")) { my $x = $value->content->__getitem__(0); if (defined $x) { $value->set($x); return $value; } } } if ($value->array) { $value->set($value->content->[0]); } elsif ($value->hash) { my @a=sort keys %{$value->content}; $value->set($value->content->{$a[0]}); } return $value; } sub fix_ampersands { my $value=shift; my $val=$value->repr(); $val=~s/&/&/g; $value->set($val); return $value; } sub floatformat { my $val=shift; return $val if not $val->number; my $value=$val->content; my $arg=shift; if ($arg and not $arg->number) { $val->set(int($value+0.5)); return $val } if ($arg) { $arg=$arg->content; } else { $arg=-1; } my $skip=$arg=~s/^-//; $value=sprintf("%.".$arg."f",$value); unless ($skip) { $value=$floatformatlocale->($value) if $floatformatlocale; $val->set($value); return $val; } $value=~s/0+$//g; $value=~s/\.$//g; $value=$floatformatlocale->($value) if $floatformatlocale; $val->set($value); return $val; } my $escape=sub { my $val=shift; $val=~s/&/&/g; $val=~s/</g; $val=~s/>/>/g; $val=~s/\"/"/g; $val=~s/\'/'/g; return $val; }; sub force_escape { my $value=shift; $value->escape(1); return Dotiac::DTL::Value->safe($value->string()); } sub get_digit { my $value=shift; return $value unless $value->number; my $val=$value->content;; my $pos = shift; return $val unless defined $pos and $pos->number; $pos=int $pos->content; return $value if $pos < 1; return Dotiac::DTL::Value->safe(0) if $pos > CORE::length($val); $value->set(substr $val,-$pos,1); return $value; } #Should only be used together with urlencode sub iriencode { my $val=shift; my $value=$val->repr; #require Encode; #$value=Encode::encode_utf8($value) if Encode::is_utf8($value); $value = eval { pack("C*", unpack("U0C*", $value))} || pack("C*", unpack("C*", $value)); $value=~s/([^a-zA-Z0-9\[\]\(\)\$\%\&\/:;#=,!\?\*_.~-])/uc sprintf("%%%02x",ord($1))/eg; $val->set($value); return $val; } sub join { my $value=shift; my $j=shift; if ($j) { $j=$j->repr; } else { $j=""; } if ($value->object) { if ($value->content->can("__len__") and $value->content->can("__getitem__")) { #No support for __iter__ right now. my @a; foreach my $i (0 .. $value->content->__len__()-1) { push @a,$value->content->__getitem__($i); } $value->set(CORE::join($j,@a)); return $value } if ($value->content->can("count") and $value->content->can("__getitem__")) { #No support for __iter__ right now. my @a; foreach my $i (0 .. $value->content->count()-1) { push @a,$value->content->__getitem__($i); } $value->set(CORE::join($j,@a)); return $value; } } $value->set(CORE::join($j,@{$value->content})) if $value->array; $value->set(CORE::join($j,values %{$value->content})) if $value->hash; return $value; } sub last { my $value=shift; if ($value->object) { if ($value->content->can("__len__") and $value->content->can("__getitem__")) { my $x = $value->content->__getitem__($value->content->__len__()-1); if (defined $x) { $value->set($x); return $value; } } if ($value->content->can("count") and $value->content->can("__getitem__")) { my $x = $value->content->__getitem__($value->content->count()-1); if (defined $x) { $value->set($x); return $value; } } } if ($value->array) { if (@{$value->content}) { $value->set($value->content->[-1]); } else { $value->set(undef); } } elsif ($value->hash) { my @a=sort keys %{$value->content}; if (@a) { $value->set($value->content->{$a[-1]}); } else { $value->set(undef); } } return $value; } sub length { my $value=shift; return Dotiac::DTL::Value->safe(0) if $value->undef; return Dotiac::DTL::Value->safe(CORE::length($value->content)) if $value->scalar; return Dotiac::DTL::Value->safe($value->content->count()) if $value->object and $value->content->can("count"); return Dotiac::DTL::Value->safe($value->content->__len__()) if $value->object and $value->content->can("__len__"); return Dotiac::DTL::Value->safe(scalar @{$value->content}) if $value->array; return Dotiac::DTL::Value->safe(scalar keys %{$value->content}) if $value->hash; return Dotiac::DTL::Value->safe(0); } #output will be 1 or 0, not True or False sub length_is { my $value=shift; my $is=shift; if ($is->number) { $is=int($is->content()); } else { $is=0; } $is = 0 unless defined $is and Scalar::Util::looks_like_number($is); return Dotiac::DTL::Value->safe(!$is) if $value->undef; return Dotiac::DTL::Value->safe(CORE::length($value->content) == $is) if $value->scalar(); return Dotiac::DTL::Value->safe($value->content->count() == $is) if $value->object and $value->content->can("count"); return Dotiac::DTL::Value->safe($value->content->__len__() == $is) if $value->object and $value->content->can("__len__"); return Dotiac::DTL::Value->safe(@{$value->content} == $is) if $value->array; return Dotiac::DTL::Value->safe(keys %{$value->content} == $is) if $value->hash; return Dotiac::DTL::Value->safe(0) } sub linebreaks { my $value=shift; $value=$value->string(); $value=~s/\n\s*\n/<\/p>
/g;
$value=~s/\n/
/g;
return Dotiac::DTL::Value->safe("
".$value."
"); } sub linebreaksbr { my $value=shift; $value=$value->string(); $value=~s/\n/bbb
, the also closes . } next; } elsif ($tag=~s/\/$//) { #XML: Singletag next; } else { $tag=~m/^(\w+)/; $tag=$1; unshift @tags,$tag unless $singletags{$tag}; #Some HTML-Tags shouldn't be closed, (why not, I wonder) next; } } else { return $val->set($ret); #Parsingerror. } } else { pos($value)=$pos; } } return $val if $words > 0; #Should be allright then. $ret=~s/\s+$//g; $ret.="..." unless $ret=~m/\.\.\.$/; foreach my $t (@tags) { $ret.="$t>"; } return $val->set($ret); } #TODO TODO TODO # Split in subfuntion ziehe safe aus $value->safe(); #TODO TODO TODO # my $unordered_list; $unordered_list = sub { my $e=shift; my $save=shift; my $level=shift; my $res=""; return "" unless ref $e and ref $e eq "ARRAY"; my @loop=@$e; while (@loop) { my $title=shift @loop; $title=$escape->($title) unless $save; $res.="\t"x($level)." like tags), but for email's from forms or other text files.
{{ "Hello"|center:"20" }} {# " Hello " #}
B
=head3 Bugs and Differences to Django
Also supports a padding parameter, if you want something other than spaces:
{{ "Hello":center:"20";"-" }} {# "-------Hello--------" #}
=head2 cut :STRING
Removes any occurences of a STRING from the value.
{{ "Hello World"|cut:"el" }} {# Hlo World #}
{{ "Hello World"|cut:"l" }} {# Heo Word #}
=head3 Bugs and Differences to Django
If you find any, please report them.
=head2 date :FORMAT
Formats a time, according to a FORMAT according to a FORMAT..
{{ "20002312"|date:"jS F Y H:i" }} {# 20th August 1970 14:11 #}
{{ post.time|date:"jS o\f F" }} {# It is the 4th of September #}
The retured value will be safe if the FORMAT string is safe.
=head3 Format options
You can combine as many of these as you like or need:
{{ var|date:"d. b." }}
=over
=item "\"
Returns the next character, regardless if it is a format character or not.
{{ var|date: "\H\e\l\l\o \W\o\r\l\d" }} {# =Hello World #}
This also means "\n" will in this case render an "n" and NOT a newline. Same for "\t","\f","\b","\r".
=item "a"
Returns whether it is AM or PM in Associated Press style: "a.m." or "p.m".
{{ var|date: "a" }} {# a.m. on in the morning#}
B
=item "A"
Returns AM or PM.
{{ var|date: "A" }} {# AM #}
B
=item "b"
Returns the current month in 3 lowercase letters.
{{ var|date: "b" }} {# dec #}
B
=item "d"
Returns the day of the month with a leading zero.
{{ var|date: "d" }} {# 01 #} to {# 31 #}
=item "D"
Returns the day of the week in 3 letters (2 letters on some locales)
{{ var|date: "D" }} {# Sun #}
B
=item "f"
Returns the time with hours and minutes, but minutes are left out if they are 0.
{{ var|date: "f" }} o'clock {# 11:30 o'clock #} {# 3 o'clock #}
=item "F"
Returns the month in long form.
{{ var|date: "F" }} {# December #}
B
=item "g"
Returns the hour in 12-hour format without leading zeros.
{{ var|date: "g" }} {# 1 #} to {# 12 #}
=item "G"
Returns the hour in 24-hour format without leading zeros.
{{ var|date: "G" }} {# 0 #} to {# 24 #}
=item "h"
Returns the hour in 12-hour format with a leading zero.
{{ var|date: "h" }} {# 01 #} to {# 12 #}
=item "H"
Returns the hour in 24-hour format with a leading zero.
{{ var|date: "H" }} {# 00 #} to {# 24 #}
=item "i"
Returns the minutes with a leading zero.
{{ var|date: "i" }} {# 00 #} to {# 60 #}
=item "j"
Returns the day of the month without leading zeros.
{{ var|date: "j" }} {# 1 #} to {# 31 #}
=item "l"
Returns the day of the week as a long text.
{{ var|date: "l" }} {# Sunday #}
B
=item "L"
Returns 1 or 0 whether it's a leap year.
{{ var|date: "L" }} {# 1 #}
=item "m"
Returns the current month as a number with leading zeros.
{{ var|date: "m" }} {# 01 #} to {# 12 #}
=item "M"
Returns the current month in 3 letters.
{{ var|date: "M" }} {# Dec #}
B
=item "n"
Returns the current month as a number without leading zeros.
{{ var|date: "m" }} {# 1 #} to {# 12 #}
=item "M"
Returns the current in Associated Press style notation.
{{ var|date: "M" }} {# Jan. #} {# March #} {# July #}
B
=item "O"
Returns the difference to Greenwich time in hours.
{{ var|date: "O" }} {# +0100 #}
=item "P"
Returns either the time in 12 hours and minutes if not zero with a.m. or p.m., midnight or noon.
{{ var|date: "P" }} {# 1 p.m. #} {# 11:56 a.m. #} {# midnight #} {# noon #}
=item "r"
Returns an RFC 2822 formatted date.
{{ var|date: "r" }} {# Sun, 28 Dec 2008 18:36:24 +0200' #}
B
=item "s"
Returns the seconds with a leading zero.
{{ var|date: "s" }} {# 00 #} to {# 59 #}
=item "S"
Returns the ordinal suffix for the day of the month.
{{ var|date: "S" }} {# st #} {# nd #} {# rd #} {# th #}
Defaults to english, B
=item "t"
Returns the number of days in the given month.
{{ var|date: "t" }} {# 28 #} to {# 31 #}
=item "T"
Returns the current timezone (needs the POSIX module)
{{ var|date: "T" }} {# CET #} {# GMT #} {# EST #}...
=item "w"
Returns the day of week as a number from 0 (Sunday) to 6 (Saturday)
{{ var|date: "w" }} {# 1 #} to {# 6 #}
=item "W"
Returns the ISO-8601 week number of year (uses the POSIX module), weeks start on monday.
{{ var|date: "w" }} {# 1 #} to {# 53 #}
=item "y"
Returns the year in two digits (with leading zeros)
{{ var|date: "y" }} {# 08 #}
=item "Y"
Returns the year in four (or more) digits (with leading zeros)
{{ var|date: "Y" }} {# 2008 #}
=item "z"
Returns the day of the year without leading zeros
{{ var|date: "z" }} {# 0 #} to {# 365 #}
=item "Z"
Returns the difference of the current timezone to GMT in seconds.
{{ var|date: "Z" }} {# -43200 #} to {# 43200 #}
=back
=head3 Bugs and Differences to Django
Since Perl has no default DateTime Object, this expects a normal unix timestamp ( result of the time() call in perl).
It also excepts the result of localtime as an array reference, this is useful for timestamps > 2038 on 32-Bit machines.
var=>[36,31,21,2,0,109,5,1,0];
{{ var|date:"jS F Y H:i" }} {# 2nd January 2009 21:31 #}
=head2 default :STRING
If the value is false (See L) it will return the STRING, otherwise the value.
{{ "Hello World"|cut:"el" }} {# Hlo World #}
{{ "Hello World"|cut:"l" }} {# Heo Word #}
=head3 Bugs and Differences to Django
Perl considers other things false as Python.
=head2 default_if_none :STRING
If the value is not defined (not found or set to C) it will return the STRING, otherwise the value.
{{ "Hello World"|cut:"el" }} {# Hlo World #}
{{ "Hello World"|cut:"l" }} {# Heo Word #}
=head3 Bugs and Differences to Django
Perl considers other things false as Python.
=head2 dictsort :PROPERTY
Sorts an array of hashes, objects or arrays by a common PROPERTY. (See C<|dictsort> for reverse sort)
Posts=>[
{title=>"I love food",text=>"I really do",category=>"My life"},
{title=>"I love TV",text=>"Even more than food",category=>"My life"},
{title=>"Simpsons",text=>"Awesome TV show",category=>"TV shows"},
{title=>"ANTM",text=>"I love this one",category=>"TV shows"},
{title=>"xkcd",text=>"The best webcomic",category=>"Webcomics"}
]
{% for x in Posts|dictsort:"category" %}...
{% endfor %}
{% for x in Posts|dictsort:"title" %}...
{% endfor %}
=head3 Bugs and Differences to Django
If PROPERTY is omitted, it just sorts by name, you can use this to sort an array of strings.
ListofWords=>["Foo","Bar","Baz"]
{% for x in Posts|dictsort %}
{{x}}
{% endfor %}
=head2 dictsortreversed :PROPERTY
Sorts an array of hashes, objects or arrays by a common PROPERTY in reverse order. (See C<|dictsort> for normal order)
Posts=>[
{title=>"I love food",text=>"I really do",category=>"My life"},
{title=>"I love TV",text=>"Even more than food",category=>"My life"},
{title=>"Simpsons",text=>"Awesome TV show",category=>"TV shows"},
{title=>"ANTM",text=>"I love this one",category=>"TV shows"},
{title=>"xkcd",text=>"The best webcomic",category=>"Webcomics"}
]
{% for x in Posts|dictsort:"category" %}...
{% endfor %}
{% for x in Posts|dictsort:"title" %}...
{% endfor %}
=head3 Bugs and Differences to Django
If PROPERTY is omitted, it just sorts by name, you can use this to sort an array of strings.
ListofWords=>["Foo","Bar","Baz"]
{% for x in Posts|dictsort %}
{{x}}
{% endfor %}
=head2 divisibleby :NUMBER
Returns 1 (true value) if the value is divisible by NUMBER.
{{ "21"|divisibleby:"7" }} {# 1 #}
{{ "45"|divisibleby:"8" }} {# 0 #}
=head3 Bugs and Differences to Django
Django's divisibleby returns a C or C. There is no binary type in perl, so it will return C<1> or C<0>
=head2 escape
Marks a string as unsafe, i.e. in need of escaping for output.
C< < >, C<< > >>, C<'>, C<"> and C<&> are converted to C<<>, C<>>, C<'>, C<"> and C<&> respectively.
B Escaping is done only once and only after all filters are applied. If you want to esacpe at this position in the filter pipeline use C.
{{ "<>"|escape }} {# <> #}
{{ "<>"|escape|cut:"&"|escape }} {# <> #} {# Escaping is done only once after all filter are applied #}
{{ "<>"|force_escape|cut:"&"|escape }} {# lt;gt; #} {# This might be what you want. #}
=head3 Bugs and Differences to Django
If you find any, please report them.
=head2 escapejs
Escapes a Javascript (JSON) String. This will not generate JSON Code out of datastructures, use L for that.
{# #} {# you will have to mark it as safe if you are generating in script tags #}
{# [1,2,3,4];
{{ var|first }} {# 1 #}
{{ "abc"|make_list|first }} {# a #}
=head3 Bugs and Differences to Django
Also returns the first value in a hash.
=head2 fix_ampersands
Replace C<&> with C<&>. See C and C for a better solution.
Doesn't mark the value safe.
var=>"Tom & Jerry";
{{ var|fix_ampersands }} {# Tom & Jerry #}
{{ var|fix_ampersands|safe }} {# Tom & Jerry #}
=head3 Bugs and Differences to Django
This is somewhat deprecated in Django and replaced by the autoescaping routines. Don't use this anymore.
=head2 floatformat :DIGITS
Formats a (float) value with variables with a number DIGITS after the dot. If DIGITS is negative, it will cut off trailing zeros (and the dot).
DIGITS defaults to -1
{{ "1.001"|floatformat }} {# 1 #}
{{ "1.001"|floatformat:"2" }} {# 1.00 #}
=head3 Bugs and Differences to Django
If you find any, please put them in the tracker or drop me a mail.
=head2 force_escape
Escapes the string at this point in the filter stack (not at the end like C)
C< < >, C<< > >>, C<'>, C<"> and C<&> are converted to C<<>, C<>>, C<'>, C<"> and C<&> respectively.
See also C
{{ "<>"|force_escape|escape }} {# <> #}
{{ "<>"|force_escape|safe }} {# <> #}
=head3 Bugs and Differences to Django
If you find any, please report them.
=head2 get_digit :NTH
Extracts the NTH digit (from the right) of an integer value.
Just returns the value if it was not an integer.
{{ "4893"|get_digit:"3" }} {# 8 #}
{{ "4893"|get_digit:"2" }} {# 9 #}
=head3 Bugs and Differences to Django
If you find any, please report them.
=head2 iriencode
Encodes Unicode characters according to rfc3987, all characters above 0x7f are encoded.
You won't need this filter if the output of the script is already Unicode.
This does not replace urlencode, but should be used in conjunction with it.
The result from iriencode of an iriencoded string will not change it anymore.
{{ "http://www.google.com/?q=\u0334%20"|iriencode }} {# http://www.google.de/?q=%CC%B4%20 #} {# %20 stayed #}
http://www.google.com/?q={{ var|urlencode|iriencode }}&hl=en {# The best way if var contains urlunsafe chars and unicode chars #}
=head3 Bugs and Differences to Django
This won't work on EBCDIC Systems for now, sadly.
If you find anything else, please report them.
=head2 join :STRING
Joins a list-value by a STRING.
var=>["Foo","Bar","!"];
{{ var|join:" : " }} {# Foo : Bar : ! #}
{{ "4893"|make_list|join:"," }} {# 4,8,9,3 #}
=head3 Bugs and Differences to Django
If you find any, please report them.
=head2 last
Returns the last element of a list. (See also C)
var=>[1,2,3,4];
{{ var|first }} {# 4 #}
{{ "abc"|make_list|first }} {# c #}
=head3 Bugs and Differences to Django
Also returns the last value in a hash.
=head2 length
Returns the length of arrays, lists or strings.
The returned value is always marked safe (not that it matters for output)
var=>[10,2,73,64];
{{ var|length }} {# 4 #}
{{ "abc"|length }} {# 3 #}
=head3 Bugs and Differences to Django
Tries to call count() on objects to get the length.
C (C in python) will be counted as "".
=head2 length_is :LENGTH
Returns 1 if the length of arrays, lists or strings is equal to LENGTH, "" otherwise.
The returned value is always marked safe (not that it matters for output)
var=>[10,2,73,64];
{% if var|length_is:"4" %}1{% else %}0{% endif %} {# 1 #}
{% if "abc"|length_is:"2" %}1{% else %}0{% endif %} {# 0 #}
=head3 Bugs and Differences to Django
Tries to call count() on objects to get the length.
Unknown datastructers (GLOBS, FILEHANDLES, SCALARREFS ... ) will never return true.
C (C in python) will be counted as "".
=head2 linebreaks
Converts newlines in the value to paragraphs () and breaks
. The output will always be a paragraph.
Two linebreaks/newlines (\n\n) start a new paragraph, a single one gets converted into a
tag.
This filter will apply escaping and return a safe string. Otherwise the
and
tags are going to be messed up
{{ "Hello\nWorld"|linebreaks }} {#
Hello
World
#}
{{ "Hello\nWorld\n\nFoo"|escape|linebreaks|safe }} {# Hello
World
<b>Foo</b>
#}
=head3 Bugs and Differences to Django
This might mess up your HTML if the variable is marked safe, this will appear if you want the user to include HTML or something like BBCode.
You will have to use C (See below) for that.
{{ "...\n\n.."|safe|linebreaks }} {# ...
..
#} {# Invalid: You can see how the tag is split up #}
{{ "...\n\n.."|safe|linebreaksbr }}
{# ...
..
#} {# Valid! #}
Many BBCode interpreters don't replace linebreaks by themselves. (In most forums for example you can as a user switch on "Post is HTML" "Post is BBCode" "Convert linebreaks")
=head2 linebreaksbr
Converts newlines into breaks
.
This filter will apply escaping and return a safe string. Otherwise the
tags are going to be messed up
{{ "Hello\nWorld"|linebreaksbr }} {# Hello
World #}
{{ "Hello\nWorld\n\nFoo"|escape|linebreaksbr|safe }} {# Hello
World
<b>Foo</b>#}
=head3 Bugs and Differences to Django
If you find any, please report them
=head2 linenumbers
Writes a linenumber before each line.
{{ "Hello\nWorld"|linenumbers }}
{# 1: Hello
2: World #}
{{ "Hello\nWorld\n\nFoo"|escape|linenumbers|safe }}
{# 1: Hello
2: World
3:
4: Foo #}
=head3 Bugs and Differences to Django
If you find any, please report them
=head2 ljust :FIELDWIDTH
Leftjustifies a text in a field of FIELDWIDTH spaces.
This is not usefull for HTML (unless in like tags), but for email's from forms or other text files.
{{ "Hello"|ljust:"20" }} {# "Hello " #}
B
=head3 Bugs and Differences to Django
Also supports a padding parameter, if you want something other than spaces:
{{ "Hello":ljust:"20";"-" }} {# "Hello---------------" #}
=head2 lower
Converts the value into lowercase.
{{ "Hello, World"|lower }} {# hello, world #}
=head3 Bugs and Differences to Django
If you find any, please report them
=head2 make_list
Splits a value into a list of characters
{% for x in "abc"|make_list %}{{ x }}{% if not forloop.last %},{% endif %}{% endfor %}{# a,b,c #}
{{ "def"|make_list|join:"," }} {% d,e,f %}
=head3 Bugs and Differences to Django
If given a parameter it splits at the parameter:
{{ "b,c,d"|make_list:","|join:" " }} {# b c d #}
=head2 phone2numeric
Converts a value into a phonenumber.
All this does is replace A-Y (without Q) with 2-9.
{{ "800-FOOBAR"|phone2numeric }}{# 800-366227 #}
{{ "Hello, World"|phone2numeric }} {% 43556, 96753 %}
=head3 Bugs and Differences to Django
This has no locale support for now, locales will have to redefine this one.
=head2 pluralize :STRING
Prints a different STRING if the value is not "1". This is very useful if you want to pluralize a value.
When STRING contains a comma ("y,ies") it will either take the first value on 1 and the other one in any other case.
The STRING defaults to "s".
1 template{{ "1"|pluralize }}, 3 template{{ "3"|pluralize }} {# 1 template, 3 templates #}
1 walrus{{ "1"|pluralize:"es" }}, 4 walrus{{ "4"|pluralize:"es" }} {# 1 walrus, 4 walruses #}
1 berr{{ "1"|pluralize:"y,ies" }}, 6 berr{{ "6"|pluralize:"y,ies" }} {# 1 berry, 6 berries #}
=head3 Bugs and Differences to Django
Since Dotiac::DTL also supports multiple arguments to filters, you can also write this:
1 cherr{{ "1"|pluralize:"y";"ies" }}, 6 cherr{{ "6"|pluralize:"y";"ies" }} {# 1 cherry, 6 cherries #}
This is useful if one of your STRINGs contains a comma.
1 {{ "1"|pluralize:"";", and that's all" }}, 2 {{ "2"|pluralize:"";", and that's all" }} {# 1, 2, and that's all #}
=head2 pprint
For Debug
=head3 Bugs and Differences to Django
Uses Data::Dumper instead of pprint
=head2 random
Returns a random element of a list. (See also C and C)
var=>[1,2,3,4];
{{ var|first }} {# 3 #} {# or 1 or 2 or 4 #}
{{ "abc"|make_list|first }} {# c #} {# or a or b #}
=head3 Bugs and Differences to Django
Also returns a random value of a hash.
=head2 removetags :TAGS
Removes HTML (XML) TAGS from the value. TAGS is a space seperated list of tags to be removed
{{ "HelloWorld
"|removetags:"b" }} {# HelloWorld
#}
{{ "HelloWorld
"|removetags:"b span" }} {# HelloWorld
#}
See C if you want to strip all tags from the value.
=head3 Bugs and Differences to Django
If you find any, please report them.
=head2 rjust :FIELDWIDTH
Rightjustifies a text in a field of FIELDWIDTH spaces.
This is not usefull for HTML (unless in like tags), but for email's from forms or other text files.
{{ "Hello"|rjust:"20" }} {# " Hello" #}
B
=head3 Bugs and Differences to Django
Also supports a padding parameter, if you want something other than spaces:
{{ "Hello":ljust:"20";"-" }} {# "---------------Hello" #}
=head2 safe
Marks a string as safe, i.e. in no need of escaping for output.
Also see C.
var="<>";
{{ var|safe }} {# <> #}
{{ var|escape|cut:"&"|safe }} {# <> #} {# Escaping is done only once after all filter are applied #}
=head3 Bugs and Differences to Django
If you find any, please report them.
=cut
=head2 slice :POSITION
Extracts a sublist out of a list from a POSITION. POSITION is a string of two number seperated by a ":"
See also L.
var=[1,2,3,4];
{{ var|slice:":2" }} {# [1, 2] #}
{{ var|slice:"1:" }} {# [2, 3, 4] #}
{{ var|slice:"1:2" }} {# [2] #}
{{ var|slice:"-2:-1" }} {# [3] #}
=head3 Bugs and Differences to Django
Also allows you to get a single item:
{{ var|slice:"3" }} {# 4 #} Same as: {{ var.3 }}
Also works on hashes. Then it slices the value list orderd by their keys.
=cut
=head2 slugify
Converts the value to lowercase, removes all non word characters, removes trailing and leading whitespaces and replaces all other spaces with a "-".
The resulting value is marked safe.
This is useful if you want to generate a save ID for something like a name an user entered, while keeping the original meaning.
{{ "Hello World"|slugify }} {# hello-world #}
{{ "Foo"|slugify }} {# bfoob #}
=head3 Bugs and Differences to Django
If you find any, please report them.
=head2 stringformat :FORMAT
FORMATs a value according to python's format rules. (str.format: L)
The leading % is dropped. ("%s" = "s"),
{{ "Hello World"|stringformat:"s" }} {# Hello World #}
{{ "3"|stringformat:"#+05b" }} {# 0b011 #}
{{ "3"|stringformat:"#+02d" }} {# +03 #}
=head3 Bugs and Differences to Django
This uses perl's sprintf, which is about the same as python's format. See L
"r" is emulated
=head2 striptags
Removes all HTML (XML) tags from the value.
{{ "HelloWorld
"|striptags }} {# HelloWorld #}
{{ "HelloWorld
"|striptags }} {# HelloWorld #}
See C if you want to remove only some tags from the value.
=head3 Bugs and Differences to Django
If you find any, please report them.
=head2 time :FORMAT
Formats a time, according to a FORMAT.
This only formats for time. See C if you want to format date and time.
{{ "20002312"|time:"H:i" }} {# 14:11 #}
{{ post.time|time:"P" }} {# noon #}
The retured value will be safe if the FORMAT string is safe.
=head3 Format options
You can combine as many of these as you like or need:
{{ var|time:"G:i A" }}
=over
=item "\"
Returns the next character, regardless if it is a format character or not.
{{ var|time: "\H\e\l\l\o \W\o\r\l\d" %} {# =Hello World #}
This also means "\n" will in this case render an "n" and NOT a newline. Same for "\t","\f","\b","\r".
=item "a"
Returns whether it is AM or PM in Associated Press style: "a.m." or "p.m".
{{ var|time: "a" }} {# a.m. on in the morning#}
B
=item "A"
Returns AM or PM.
{{ var|time: "A" }} {# AM #}
B
=item "f"
Returns the time with hours and minutes, but minutes are left out if they are 0.
{{ var|time: "f" }} o'clock {# 11:30 o'clock #} {# 3 o'clock #}
=item "g"
Returns the hour in 12-hour format without leading zeros.
{{ var|time: "g" }} {# 1 #} to {# 12 #}
=item "G"
Returns the hour in 24-hour format without leading zeros.
{{ var|time: "G" }} {# 0 #} to {# 24 #}
=item "h"
Returns the hour in 12-hour format with a leading zero.
{{ var|time: "h" }} {# 01 #} to {# 12 #}
=item "H"
Returns the hour in 24-hour format with a leading zero.
{{ var|time: "H" }} {# 00 #} to {# 24 #}
=item "i"
Returns the minutes with a leading zero.
{{ var|time: "i" }} {# 00 #} to {# 60 #}
=item "O"
Returns the difference to Greenwich time in hours.
{{ var|time: "O" }} {# +0100 #}
=item "P"
Returns either the time in 12 hours and minutes if not zero with a.m. or p.m., midnight or noon.
{{ var|time: "P" }} {# 1 p.m. #} {# 11:56 a.m. #} {# midnight #} {# noon #}
=item "s"
Returns the seconds with a leading zero.
{{ var|time: "s" }} {# 00 #} to {# 59 #}
=item "Z"
Returns the difference of the current timezone to GMT in seconds.
{{ var|time: "Z" }} {# -43200 #} to {# 43200 #}
=back
=head3 Bugs and Differences to Django
Since Perl has no default DateTime Object, this expects a normal unix timestamp ( result of the time() call in perl).
It also excepts the result of localtime as an array reference, this is useful for timestamps > 2038 on 32-Bit machines.
var=>[36,31,21,2,0,109,5,1,0];
{{ var|time:"H:i" }} {# 21:31 #}
=head2 timesince :REFERNCETIME
Formats a time value and displays the time since REFERENCE TIME has passed.
REFERENCETIME is C if it is omitted
If you have a past event and want to display the time since then you can use this filter.
For any time in the future it will return 0 Minutes
{{ post.date|timesince }} {# 3 years #}
{{ post.date|timesince:edit.date }} {# 3 minutes #} {# after the post #}
C and C only differ in the order of the arguments:
{{ date1|timeuntil:date2 }} == {{ date2|timesince:date1 }}
The generated value is always marked as safe.
=head3 Bugs and Differences to Django
Like C