Skip to content

Commit

Permalink
- remove errstr stuff, use exception instead.
Browse files Browse the repository at this point in the history
- do not pass template path at creating new instance.
  • Loading branch information
tokuhirom committed Aug 7, 2010
1 parent bc5ae95 commit 542edbc
Show file tree
Hide file tree
Showing 19 changed files with 193 additions and 158 deletions.
4 changes: 2 additions & 2 deletions eg/sinatraish/lib/Sinatraish.pm
Expand Up @@ -56,8 +56,8 @@ has res => (
sub render {
my ($self, $path, @args) = @_;

my $tfall = "Tfall::$view_class"->new($path, @view_opt);
my $html = $tfall->render(@args) or die $tfall->errstr;
my $tfall = "Tfall::$view_class"->new(@view_opt);
my $html = $tfall->render($path, @args) or die $tfall->errstr;
$self->res->header('Content-Length' => length($html));
$self->res->body($html);
}
Expand Down
13 changes: 12 additions & 1 deletion lib/Tfall.pm
Expand Up @@ -22,7 +22,8 @@ sub new {
$loaded{$klass}++ or do {
$klass->use or die $@;
};
return $klass->new($path, @args);
my $tmpl = $klass->new(@args);
return bless { tmpl => $tmpl, path => $path}, $class;
} else {
Carp::croak("Cannot detect file type from file name: $path");
}
Expand All @@ -31,6 +32,16 @@ sub new {
}
}

sub render {
my ($self, @args) = @_;
return $self->{tmpl}->render($self->{path}, @args);
}

sub errstr {
my ($self) = @_;
$self->{tmpl}->errstr;
}

sub is_registered {
my ($class, $ext) = @_;
return exists $MAP->{lc $ext};
Expand Down
26 changes: 0 additions & 26 deletions lib/Tfall/Base.pm

This file was deleted.

28 changes: 14 additions & 14 deletions lib/Tfall/HTML/Template.pm
@@ -1,23 +1,23 @@
package Tfall::HTML::Template;
use strict;
use warnings;
use parent qw/Tfall::Base/;
use HTML::Template;
use Try::Tiny;

sub new {
my $class = shift;
my %args = @_ == 1 ? %{$_[0]} : @_;
bless {%args}, $class;
}

sub render {
my ($self, $args) = @_;
try {
my $type = ref $self->{stuff} ? 'scalarref' : 'filename';
my $ht = HTML::Template->new($type => $self->{stuff}, @{$self->{args}});
while (my ($k, $v) = each %$args) {
$ht->param($k => $v);
}
return $ht->output();
} catch {
$self->errstr($_);
return undef;
};
my ($self, $stuff, $args) = @_;

my $type = ref $stuff ? 'scalarref' : 'filename';
my $ht = HTML::Template->new($type => $stuff, %$self);
while (my ($k, $v) = each %$args) {
$ht->param($k => $v);
}
return $ht->output();
}

1;
17 changes: 12 additions & 5 deletions lib/Tfall/TT.pm
@@ -1,15 +1,22 @@
package Tfall::TT;
use strict;
use warnings;
use parent qw/Tfall::Base/;
use Template;

sub new {
my $class = shift;

my $tt = Template->new(@_);
bless {
tt => $tt,
}, $class;
}

sub render {
my ($self, @args) = @_;
my ($self, $stuff, @args) = @_;

my $tt = Template->new(@{$self->{args}});
$tt->process( $self->{stuff}, @args, \my $out )
or do { $self->errstr( $tt->error ); return };
$self->{tt}->process( $stuff, @args, \my $out )
or do { $self->errstr( $self->{tt}->error ); return };
$out;
}

Expand Down
20 changes: 10 additions & 10 deletions lib/Tfall/Template/Semantic.pm
@@ -1,19 +1,19 @@
package Tfall::Template::Semantic;
use strict;
use warnings;
use parent qw/Tfall::Base/;
use Template::Semantic;
use Try::Tiny;

sub new {
my $class = shift;
my $ts = Template::Semantic->new(@_);
bless {
ts => $ts,
}, $class;
}

sub render {
my ($self, @args) = @_;
try {
my $ts = Template::Semantic->new(@{$self->{args}});
$ts->process( $self->{stuff}, @args);
} catch {
$self->errstr($_);
return undef;
};
my ($self, $stuff, @args) = @_;
$self->{ts}->process( $stuff, @args);
}

1;
19 changes: 12 additions & 7 deletions lib/Tfall/Text/Markdown.pm
@@ -1,16 +1,21 @@
package Tfall::Text::Markdown;
use strict;
use warnings;
use parent qw/Tfall::Base/;
use Text::Markdown;
use Tfall::Util qw/slurp/;

sub new {
my $class = shift;
my $mkdn = Text::Markdown->new(@_);
bless {
mkdn => $mkdn,
}, $class;
}

sub render {
my ($self, @args) = @_;
if (defined (my $src = $self->slurp)) {
return Text::Markdown->new(@{ $self->args })->markdown($src);
} else {
return undef;
}
my ($self, $stuff, @args) = @_;
my $src = slurp($stuff);
return $self->{mkdn}->markdown($src);
}

1;
26 changes: 13 additions & 13 deletions lib/Tfall/Text/MicroMason.pm
@@ -1,23 +1,23 @@
package Tfall::Text::MicroMason;
use strict;
use warnings;
use parent 'Tfall::Base';
use Text::MicroMason;
use Try::Tiny;

sub new {
my $class = shift;
my $mason = Text::MicroMason->new(@_);
bless {
mason => $mason,
}, $class;
}

sub render {
my ($self, @args) = @_;
my ($self, $stuff, @args) = @_;

try {
my $mason = Text::MicroMason->new(@{$self->args});
$mason->execute(
(ref $self->stuff ? ('text' => ${$self->stuff}) : ('file' => $self->stuff)),
@args
);
} catch {
$self->errstr($_);
return undef;
}
$self->{mason}->execute(
(ref $stuff ? ('text' => ${$stuff}) : ('file' => $stuff)),
@args
);
}

1;
32 changes: 17 additions & 15 deletions lib/Tfall/Text/MicroTemplate/File.pm
@@ -1,24 +1,26 @@
package Tfall::Text::MicroTemplate::File;
use strict;
use warnings;
use parent 'Tfall::Base';
use Text::MicroTemplate::File;
use Try::Tiny;

sub new {
my $class = shift;
my $mtf = Text::MicroTemplate::File->new(@_);
bless {
mtf => $mtf,
}, $class;
}

sub render {
my ($self, @args) = @_;
my $mtf = Text::MicroTemplate::File->new(@{$self->{args}});
try {
if (ref $self->stuff) {
$mtf->parse(${$self->stuff});
$mtf->build()->(@args);
} else {
$mtf->render_file($self->stuff, @args);
}
} catch {
$self->errstr($_);
return undef;
};
my ($self, $stuff, @args) = @_;

my $mtf = $self->{mtf};
if (ref $stuff) {
$mtf->parse(${$stuff});
$mtf->build()->(@args);
} else {
$mtf->render_file($stuff, @args);
}
}

1;
Expand Down
21 changes: 13 additions & 8 deletions lib/Tfall/Text/Sass.pm
@@ -1,17 +1,22 @@
package Tfall::Text::Sass;
use strict;
use warnings;
use parent qw/Tfall::Base/;
use Text::Sass;
use Tfall::Util qw/slurp/;

sub new {
my $class = shift;
my $sass = Text::Sass->new();
bless {
sass => $sass,
}, $class;
}

sub render {
my ($self, @args) = @_;
if (defined (my $src = $self->slurp)) {
my $sass = Text::Sass->new();
return $sass->sass2css($src);
} else {
return undef;
}
my ($self, $stuff, @args) = @_;
my $sass = $self->{sass};
my $src = slurp($stuff);
return $sass->sass2css($src);
}

1;
20 changes: 10 additions & 10 deletions lib/Tfall/Text/Xslate.pm
@@ -1,21 +1,21 @@
package Tfall::Text::Xslate;
use strict;
use warnings;
use parent qw/Tfall::Base/;
use Text::Xslate;
use Try::Tiny;

sub new {
my $class = shift;

my $xslate = Text::Xslate->new(@_);
bless {
xslate => $xslate,
}, $class;
}

sub render {
my ($self, @args) = @_;

my $tt = Text::Xslate->new(@{$self->{args}});
my $out;
try {
my $out = $tt->process( $self->{stuff}, @args );
} catch {
$self->errstr( $_ );
};
$out;
return $self->{xslate}->render(@args);
}

1;
18 changes: 18 additions & 0 deletions lib/Tfall/Util.pm
@@ -0,0 +1,18 @@
package Tfall::Util;
use strict;
use warnings;
use Exporter 'import';

our @EXPORT = qw/slurp/;

sub slurp {
my $stuff = shift;
return ${$stuff} if ref $stuff;

open my $fh, '<', $stuff or do {
die "Cannot open file '@{[ $stuff ]}': $!";
};
do { local $/; <$fh> };
}

1;
16 changes: 9 additions & 7 deletions t/020_templates/01_tt.t
Expand Up @@ -9,19 +9,21 @@ use Tfall;
is $tmpl->render({name => 'john'}), "Hello, john.\n";
}
{
my $tmpl = Tfall->new('t/tmpl/unknown.tt');
is $tmpl->render({name => 'john'}), undef;
ok $tmpl->errstr();
eval {
my $tmpl = Tfall->new('t/tmpl/unknown.tt');
is $tmpl->render({name => 'john'}), undef;
};
ok $@;
}

{
my $tmpl = Tfall::TT->new('t/tmpl/foo.tt');
is $tmpl->render({name => 'john'}), "Hello, john.\n";
my $tmpl = Tfall::TT->new();
is $tmpl->render('t/tmpl/foo.tt', {name => 'john'}), "Hello, john.\n";
}

{
my $tmpl = Tfall::TT->new(\"Hello, [% name %].");
is $tmpl->render({name => 'john'}), "Hello, john.";
my $tmpl = Tfall::TT->new();
is $tmpl->render(\"Hello, [% name %].", {name => 'john'}), "Hello, john.";
}

done_testing;
Expand Down

0 comments on commit 542edbc

Please sign in to comment.