Catalyst::View::HTML::Template - HTML::Template View Class


Catalyst-View-HTML-Template documentation Contained in the Catalyst-View-HTML-Template distribution.

Index


Code Index:

NAME

Top

Catalyst::View::HTML::Template - HTML::Template View Class

SYNOPSIS

Top

    # use the helper
    create.pl view HTML::Template HTML::Template

    # lib/MyApp/View/HTML/Template.pm
    package MyApp::View::HTML::Template;

    use base 'Catalyst::View::HTML::Template';

    __PACKAGE__->config(
        die_on_bad_params => 0,
        file_cache        => 1,
        file_cache_dir    => '/tmp/cache'
    );

    1;

    # Meanwhile, maybe in an 'end' action
    $c->forward('MyApp::View::HTML::Template');




DESCRIPTION

Top

This is the HTML::Template view class. Your subclass should inherit from this class.

METHODS

process

Renders the template specified in $c->stash->{template} or $c->request->match. Template params are set up from the contents of $c->stash, augmented with base set to $c->req->base and name to $c->config->{name}. Output is stored in $c->response->body.

render

Renders the given template and returns output. Template params are set up either from the contents of %$args if $args is a hashref, or $c->stash, augmented with base set to $c->req->base and name to $c->config->{name}.

config

This allows your view subclass to pass additional settings to the HTML::Template config hash.

SEE ALSO

Top

HTML::Template, Catalyst, Catalyst::Base.

AUTHOR

Top

Christian Hansen, ch@ngmedia.com

COPYRIGHT

Top


Catalyst-View-HTML-Template documentation Contained in the Catalyst-View-HTML-Template distribution.
package Catalyst::View::HTML::Template;

use strict;
use base 'Catalyst::View';

use HTML::Template;

our $VERSION = '0.03';

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

    my $filename = $c->stash->{template} || $c->req->match;
    my $body = $self->render($c,$filename);

    unless ( $c->response->headers->content_type ) {
        $c->res->headers->content_type('text/html; charset=utf-8');
    }

    $c->response->body($body);

    return 1;
}

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

    unless ($filename) {
        $c->log->debug('No template specified for rendering') if $c->debug;
        return 0;
    }

    my %options = (
        cache    => 1,
        filename => $filename,
        path     => [ $c->path_to('root'), $c->path_to('root','base') ],
    );

    $c->log->debug(qq/Rendering template "$filename"/) if $c->debug;

    my $template = HTML::Template->new( %options, %{ $self } );

    my $template_params = $args && ref($args) eq 'HASH' ? $args : $c->stash;

    $template->param(
        base => $c->req->base,
        name => $c->config->{name},
        %$template_params
    );

    my $output;

    eval { $output = $template->output };

    if ( my $error = $@ ) {
        chomp $error;
        $error = qq/Couldn't render template "$filename". Error: "$error"/;
        $c->log->error($error);
        $c->error($error);
        return 0;
    }
    return $output;
}

1;