Skip to content

Commit

Permalink
document capture function
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Apr 24, 2012
1 parent 64a6b89 commit a65518b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
16 changes: 10 additions & 6 deletions lib/Mojo/Template.pm
Expand Up @@ -28,16 +28,13 @@ has tree => sub { [] };
my $HELPERS = <<'EOF';
use Mojo::ByteStream 'b';
use Mojo::Util;
no strict 'refs';
no warnings 'redefine';
sub capture;
*capture = sub { shift->(@_) };
sub escape;
*escape = sub {
sub capture { shift->(@_) }
sub escape {
return $_[0] if ref $_[0] eq 'Mojo::ByteStream';
no warnings 'uninitialized';
Mojo::Util::xml_escape "$_[0]";
};
}
use Mojo::Base -strict;
EOF
$HELPERS =~ s/\n//g;
Expand Down Expand Up @@ -448,6 +445,13 @@ C<end> keywords.
<%= $block->('Baerbel') %>
<%= $block->('Wolfgang') %>
Or use the C<capture> function to get the result right away.
% use Mojo::Util 'trim';
<%= trim capture begin %>
<p>Just some HTML</p>
<% end %>
=head2 Indentation
Perl lines can also be indented freely.
Expand Down
12 changes: 11 additions & 1 deletion t/mojo/template.t
Expand Up @@ -17,7 +17,7 @@ use Mojo::Base -strict;

use utf8;

use Test::More tests => 200;
use Test::More tests => 201;

# "When I held that gun in my hand, I felt a surge of power...
# like God must feel when he's holding a gun."
Expand Down Expand Up @@ -267,6 +267,16 @@ $output = $mt->render(<<'EOF');
EOF
is $output, "<html>\n\n", 'escaped expression block';

# Capture and filter block
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
% use Mojo::Util 'trim';
<%= trim capture begin %>
<p>Just some HTML</p>
<% end %>
EOF
is $output, "<p>Just some HTML</p>\n", 'captured lines';

# Capture lines (passed through with extra whitespace)
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
Expand Down

0 comments on commit a65518b

Please sign in to comment.