| 1 | package Mason::Filters::Standard; | ||
| 2 | $Mason::Filters::Standard::VERSION = '2.23'; | ||
| 3 | use Mason::DynamicFilter; | ||
| 4 | use Mason::Util; | ||
| 5 | use Mason::PluginRole; | ||
| 6 | |||
| 7 | method Capture ($outref) { | ||
| 8 | sub { $$outref = $_[0]; return '' } | ||
| 9 | } | ||
| 10 | |||
| 11 | method CompCall ($path, @params) { | ||
| 12 | Mason::DynamicFilter->new( | ||
| 13 | filter => sub { | ||
| 14 | my $m = $self->m; | ||
| 15 | return $m->scomp( $path, @params, yield => $_[0] ); | ||
| 16 | } | ||
| 17 | ); | ||
| 18 | } | ||
| 19 | |||
| 20 | method NoBlankLines () { | ||
| 21 | sub { | ||
| 22 | my $text = $_[0]; | ||
| 23 | $text =~ s/^\s*\n//mg; | ||
| 24 | return $text; | ||
| 25 | }; | ||
| 26 | } | ||
| 27 | |||
| 28 | method Repeat ($times) { | ||
| 29 | Mason::DynamicFilter->new( | ||
| 30 | filter => sub { | ||
| 31 | my $content = ''; | ||
| 32 | for ( my $i = 0 ; $i < $times ; $i++ ) { | ||
| 33 | $content .= $_[0]->(); | ||
| 34 | } | ||
| 35 | return $content; | ||
| 36 | } | ||
| 37 | ); | ||
| 38 | } | ||
| 39 | |||
| 40 | method Tee ($outref) { | ||
| 41 | sub { $$outref = $_[0]; return $_[0] } | ||
| 42 | } | ||
| 43 | |||
| 44 | method Trim () { | ||
| 45 | sub { Mason::Util::trim( $_[0] ) } | ||
| 46 | } | ||
| 47 | |||
| 48 | 1; | ||
| 49 | |||
| 50 | __END__ | ||
| 51 | |||
| 52 | =pod | ||
| 53 | |||
| 54 | =head1 NAME | ||
| 55 | |||
| 56 | Mason::Filters::Standard - Standard filters | ||
| 57 | |||
| 58 | =head1 DESCRIPTION | ||
| 59 | |||
| 60 | These filters are automatically composed into | ||
| 61 | L<Mason::Component|Mason::Component>. | ||
| 62 | |||
| 63 | =head1 FILTERS | ||
| 64 | |||
| 65 | =over | ||
| 66 | |||
| 67 | =item Capture ($ref) | ||
| 68 | |||
| 69 | Uses C<< $m->capture >> to capture the content in I<$ref> instead of outputting | ||
| 70 | it. | ||
| 71 | |||
| 72 | % $.Capture(\my $content) {{ | ||
| 73 | <!-- this will end up in $content --> | ||
| 74 | % }} | ||
| 75 | |||
| 76 | ... do something with $content | ||
| 77 | |||
| 78 | =item CompCall ($path, @args...) | ||
| 79 | |||
| 80 | Calls the component with I<path> and I<@args>, just as with C<< $m->scomp >>, | ||
| 81 | with an additional coderef argument C<yield> that can be invoked to generate | ||
| 82 | the content. Arguments passed to C<yield> can be accessed inside the content | ||
| 83 | via C<@_>. This is the replacement for Mason 1's L<Components With | ||
| 84 | Content|http://search.cpan.org/perldoc?HTML::Mason::Devel#Component_Calls_with_Content>. | ||
| 85 | |||
| 86 | In index.mc: | ||
| 87 | % $.CompCall ('list_items.mi', items => \@items) {{ | ||
| 88 | <li><% $_[0] %></li> | ||
| 89 | % }} | ||
| 90 | |||
| 91 | In list_items.mi: | ||
| 92 | <%class> | ||
| 93 | has 'items'; | ||
| 94 | has 'yield'; | ||
| 95 | </%class> | ||
| 96 | |||
| 97 | % foreach my $item (@{$.items}) { | ||
| 98 | <% $.yield->($item) %> | ||
| 99 | % } | ||
| 100 | |||
| 101 | =item NoBlankLines | ||
| 102 | |||
| 103 | Remove lines with only whitespace from content. This | ||
| 104 | |||
| 105 | % $.NoBlankLines {{ | ||
| 106 | |||
| 107 | hello | ||
| 108 | |||
| 109 | |||
| 110 | world | ||
| 111 | % }} | ||
| 112 | |||
| 113 | yields | ||
| 114 | |||
| 115 | hello | ||
| 116 | world | ||
| 117 | |||
| 118 | =item Repeat ($count) | ||
| 119 | |||
| 120 | Repeat the content block I<$count> times. Note that the block is re-executed | ||
| 121 | each time, which may result in different content. | ||
| 122 | |||
| 123 | <!-- Prints 1 to 5 --> | ||
| 124 | % my $i = 1; | ||
| 125 | % $.Repeat(5) {{ | ||
| 126 | <% $i++ %><br> | ||
| 127 | % }} | ||
| 128 | |||
| 129 | =item Tee ($ref) | ||
| 130 | |||
| 131 | Uses C<< $m->capture >> to capture the content in I<$ref>, and also output it. | ||
| 132 | |||
| 133 | % $.Tee(\my $content) {{ | ||
| 134 | <!-- this will end up in $content and also be output --> | ||
| 135 | % }} | ||
| 136 | |||
| 137 | ... | ||
| 138 | |||
| 139 | <!-- output content again down here --> | ||
| 140 | <% $content %> | ||
| 141 | |||
| 142 | =item Trim | ||
| 143 | |||
| 144 | Remove whitespace from the beginning and end of the content. | ||
| 145 | |||
| 146 | =back | ||
| 147 | |||
| 148 | =head1 SEE ALSO | ||
| 149 | |||
| 150 | L<Mason::Manual::Filters|Mason::Manual::Filters>, L<Mason|Mason> | ||
| 151 | |||
| 152 | =head1 AUTHOR | ||
| 153 | |||
| 154 | Jonathan Swartz <swartz@pobox.com> | ||
| 155 | |||
| 156 | =head1 COPYRIGHT AND LICENSE | ||
| 157 | |||
| 158 | This software is copyright (c) 2012 by Jonathan Swartz. | ||
| 159 | |||
| 160 | This is free software; you can redistribute it and/or modify it under | ||
| 161 | the same terms as the Perl 5 programming language system itself. | ||
| 162 | |||
| 163 | =cut |