Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
removed render_to_file and render_file_to_file methods from Mojo::Tem…
…plate
  • Loading branch information
kraih committed Jun 24, 2012
1 parent 50dddbe commit 1ce74ed
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 77 deletions.
2 changes: 2 additions & 0 deletions Changes
Expand Up @@ -6,6 +6,8 @@
- Removed app_class attribute from Mojo::Server.
- Removed qp_decode and qp_encode methods from Mojo::ByteStream.
- Removed qp_decode and qp_encode functions from Mojo::Util.
- Removed render_to_file and render_file_to_file methods from
Mojo::Template.
- Renamed Mojo::CookieJar to Mojo::UserAgent::CookieJar.
- Renamed Mojo::Command to Mojolicious::Command.
- Merged get_all_data and get_data methods from Mojo::Command into data
Expand Down
39 changes: 0 additions & 39 deletions lib/Mojo/Template.pm
Expand Up @@ -307,18 +307,6 @@ sub render_file {
return $self->render($tmpl, @_);
}

sub render_file_to_file {
my ($self, $spath, $tpath) = (shift, shift, shift);
my $output = $self->render_file($spath, @_);
return ref $output ? $output : $self->_write_file($tpath, $output);
}

sub render_to_file {
my ($self, $tmpl, $path) = (shift, shift, shift);
my $output = $self->render($tmpl, @_);
return ref $output ? $output : $self->_write_file($path, $output);
}

sub _trim {
my ($self, $line) = @_;

Expand All @@ -343,18 +331,6 @@ sub _trim {
}
}

sub _write_file {
my ($self, $path, $output) = @_;

# Encode and write to file
croak qq{Can't open file "$path": $!} unless open my $file, '>', $path;
$output = encode $self->encoding, $output if $self->encoding;
croak qq{Can't write to file "$path": $!}
unless defined $file->syswrite($output);

return;
}

1;

=head1 NAME
Expand Down Expand Up @@ -704,21 +680,6 @@ Render template.
Render template file.
=head2 C<render_file_to_file>
my $exception = $mt->render_file_to_file('/tmp/foo.mt', '/tmp/foo.txt');
my $exception
= $mt->render_file_to_file('/tmp/foo.mt', '/tmp/foo.txt', @args);
Render template file to another file.
=head2 C<render_to_file>
my $exception = $mt->render_to_file($template, '/tmp/foo.txt');
my $exception = $mt->render_to_file($template, '/tmp/foo.txt', @args);
Render template to file.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
Expand Down
41 changes: 3 additions & 38 deletions t/mojo/template.t
Expand Up @@ -17,7 +17,7 @@ use Mojo::Base -strict;

use utf8;

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

# "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 @@ -1061,39 +1061,6 @@ is $output->lines_after->[0]->[0], 3, 'right number';
is $output->lines_after->[0]->[1], '123', 'right line';
like "$output", qr/foo\.mt line 2/, 'right result';

# Exception in file (rendering to file)
$mt = Mojo::Template->new;
my $dir = File::Temp::tempdir(CLEANUP => 1);
my $file2 = catfile $dir, 'test1.mt';
$output = $mt->render_file_to_file($file, $file2);
ok !-e $file2, 'file has not been rendered';
isa_ok $output, 'Mojo::Exception', 'right exception';
like $output->message, qr/exception\.mt line 2/, 'message contains filename';
is $output->lines_before->[0]->[0], 1, 'right number';
is $output->lines_before->[0]->[1], 'test', 'right line';
is $output->line->[0], 2, 'right number';
is $output->line->[1], '% die;', 'right line';
is $output->lines_after->[0]->[0], 3, 'right number';
is $output->lines_after->[0]->[1], '123', 'right line';
like "$output", qr/exception\.mt line 2/, 'right result';

# File to file with utf8 data
$mt = Mojo::Template->new;
$mt->tag_start('[$-');
$mt->tag_end('-$]');
$file = catfile $dir, 'test.mt';
is $mt->render_to_file(<<"EOF", $file), undef, 'file rendered';
<% my \$i = 23; %> foo bar
\x{df}\x{0100}bar\x{263a} <%= \$i %>
test
EOF
$mt = Mojo::Template->new;
$file2 = catfile $dir, 'test2.mt';
is $mt->render_file_to_file($file, $file2), undef, 'file rendered to file';
$mt = Mojo::Template->new;
$output = $mt->render_file($file2);
is $output, " foo bar\n\x{df}\x{0100}bar\x{263a} 23\ntest\n", 'right result';

# Exception with utf8 context
$mt = Mojo::Template->new;
$file = catfile(splitdir($FindBin::Bin), qw(lib utf8_exception.mt));
Expand All @@ -1107,9 +1074,7 @@ is utf8::is_utf8($output->line->[1]), 1, 'context has utf8 flag';
is utf8::is_utf8($output->lines_after->[0]->[1]), 1, 'context has utf8 flag';

# Different encodings
$mt = Mojo::Template->new(encoding => 'ISO-8859-1');
$file = catfile $dir, 'test3.mt';
is $mt->render_to_file('ü', $file), undef, 'file rendered';
$mt = Mojo::Template->new(encoding => 'UTF-8');
$mt = Mojo::Template->new(encoding => 'shift_jis');
$file = catfile(splitdir($FindBin::Bin), qw(lib utf8_exception.mt));
ok !eval { $mt->render_file($file) }, 'file not rendered';
like $@, qr/invalid encoding/, 'right error';

0 comments on commit 1ce74ed

Please sign in to comment.