package SimpleMaps::WebApp::Template;

use strict;

use Carp qw( croak );
use CGI;
use Template;

=head1 NAME

SimpleMaps::WebApp::Template - Do Template Toolkit related stuff for web-based SimpleMaps applications.

=cut

sub output {
    my ($class, %args) = @_;
    croak "No template supplied" unless $args{template};
    my $template_path = "/home/kake/public_html/cgi-bin/mapping-simple/templates/";
    my $tt = Template->new( { INCLUDE_PATH => $template_path } );
    my $tt_vars = $args{vars} || {};

    my $header = "";
    unless ( defined $args{content_type} and $args{content_type} eq "" ) {
      $header = CGI::header( -cookie => $args{cookies} );
    }
    my $output;
    $tt->process( $args{template}, $tt_vars, \$output );

    $output ||= qq(<html><head><title>ERROR</title></head><body><p>
                   Failed to process template: )
              . $tt->error
              . qq(</p></body></html>);

    return $header . $output;
}

1;
