RunApp::Template - Base class for RunApp template service


RunApp documentation Contained in the RunApp distribution.

Index


Code Index:

NAME

Top

RunApp::Template - Base class for RunApp template service

SYNOPSIS

Top

  my $cron = RunApp::Template->new(
    file => "file to be generated",
    source => "template file",
    .. other params, passed to template ..
  );




DESCRIPTION

Top

The class allows inherited classes to use Template, and will use the DATA section of the subclass as default template if not otherwise specified.

AUTHORS

Top

Chia-liang Kao <clkao@clkao.org>

Refactored from works by Leon Brocard <acme@astray.com> and Tom Insam <tinsam@fotango.com>.

COPYRIGHT

Top


RunApp documentation Contained in the RunApp distribution.

package RunApp::Template;
use strict;
use base qw(RunApp::Base);
use Template;

sub new {
  my $class = shift;
  my $self = $class->SUPER::new (@_);
  unless ($self->{source}) {
    my $source = $self->{file}.'.default';
    if (-e $source) {
      $self->{source} = $source;
    }
    else {
      $self->{source} = $self->get_data;
    }
  }
  return $self;
}

sub get_template {
  Template->new({ ABSOLUTE => 1,
                  DEBUG_UNDEF => 1 });
}

sub build {
  my ($self, $conf) = @_;
  #warn ". building $self->{file}\n";
  my $tt = $self->get_template ($conf);
  $self->{source} ||= $self->{file}.'.default';
  $tt->process($self->{source}, {%$conf, PACKAGE => ref($self)}, $self->{file})
      or die $tt->error(), "\n";
}

sub get_data {
  my ($self) = @_;
  my $class = ref $self || $self;
  no strict 'refs';
  local $/;
  my $data = "${class}::DATA";
  ${$class.'::_DATA'} ||= \<$data>;
}

1;