Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: e59d5b0487
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 184 lines (139 sloc) 3.763 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
package Angelos::View;
use Mouse;
use Angelos::Home;
use Angelos::MIMETypes;
use Path::Class qw(file dir);
use Angelos::Exceptions;

with( 'Angelos::Component', );

has _plugin_app_ns => ( +default => sub { ['Angelos::View'] }, );

has 'context' => ( is => 'rw', );

has 'types' => (
    is => 'rw',
    default => sub {
        Angelos::MIMETypes->new;
    }
);

has 'root' => (
    is => 'rw',
    default => sub {
        Angelos::Home->path_to( 'share', 'root', 'templates' );
    },
);

has 'CONTENT_TYPE' => (
    is => 'rw',
    isa => 'Str',
);

has 'TEMPLATE_EXTENSION' => (
    is => 'rw',
    required => 1,
);

has 'engine' => ( is => 'rw', );

sub BUILD {
    my $self = shift;
    my $template_engine = $self->_build_engine;
    $self->engine($template_engine) if $template_engine;
}

no Mouse;

sub SETUP { }

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

sub RENDER {
    my ( $self, $args ) = @_;
    my $c = $self->context;
    my $template = $self->_template($c);
    my $template_path = $self->_template_path($c);
    return undef unless $template || $template_path;

    $self->_build_stash($c);
    my $vars = $self->_build_template_vars( $c, $args );
    my $output = $self->_do_render( $c, $vars );
    return undef unless $output;

    $self->_build_response( $c, $output );
    return 1;
}

sub _build_template_vars {
    my ( $self, $c, $args ) = @_;
    my $vars = {
        ( ref $args eq 'HASH' ? %$args : %{ $c->stash() } ),
        $self->_template_vars($c)
    };
}

sub _template_vars {
    my ( $self, $c ) = @_;
    ( c => $c,
        base => $c->req->base,
    );
}

sub _build_stash {
    my ( $self, $c ) = @_;
    $c->stash->{c} = $self->context;
    $c->stash->{base} ||= $c->req->base;
}

sub _do_render {
    my ( $self, $c, $vars ) = @_;
    my $output = eval { $self->_render( $c, $vars ); };
    $output;
}

sub _render {
    Angelos::Exception::AbstractMethod->throw(
        message => 'Sub class must overried this method' );
}

sub _build_engine {
    Angelos::Exception::AbstractMethod->throw(
        message => 'Sub class must overried this method' );
}

sub _build_response {
    my ( $self, $c, $output ) = @_;

    $c->res->code(200) unless $c->res->code;
    $c->res->body($output);

    unless ( $c->res->content_type ) {

        # guess extension from request path
        my $ct
            = $self->_content_type( $c->stash->{format}
                || $c->_match->params->{format}
                || 'html' );
        my $charset = 'utf-8';
        $c->res->content_type("$ct; charset=$charset");
    }
    $c->res;
}

sub _template {
    my ( $self, $c ) = @_;
    my $template ||= $c->stash->{template};

    $template
        ||= lc( $c->_match->params->{controller} ) . "/"
        . $c->_match->params->{action}
        . $self->TEMPLATE_EXTENSION;

    # FIXME
    $c->stash->{template} = $template;
    $template;
}

sub _template_path {
    my ( $self, $c ) = @_;
    my $template = $c->stash->{template};
    my $template_path = $c->stash->{template_path};
    if ( $template && !$template_path ) {
        my $path = file( $self->root, $template );
        $c->stash->{template_path} = $path;
    }
    $c->stash->{template_path};
}

sub _content_type {
    my ( $self, $format ) = @_;
    return $self->CONTENT_TYPE if $self->CONTENT_TYPE;
    $self->types->mime_type_of($format);
}

__PACKAGE__->meta->make_immutable;

1;

__END__

=head1 NAME


=head1 SYNOPSIS

=head1 DESCRIPTION


=head1 AUTHOR

Takatoshi Kitano E<lt>kitano.tk@gmail.comE<gt>

=head1 SEE ALSO

=head1 LICENSE

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut
Something went wrong with that request. Please try again.