CGI-Template

 view release on metacpan or  search on metacpan

lib/CGI/Template.pm  view on Meta::CPAN

		} else {
			croak "CGI::Template : Invalid request argument passed to new(): $request_method";
		}
	}
		

	$doctype = "" unless $doctype;

	if( $doctype =~ /^transitional$/i || $doctype =~ /^loose$/i ){
		$DOCTYPE = $TRANSITIONAL;

	} elsif( $doctype =~ /^frameset$/i ){
		$DOCTYPE = $FRAMESET;

	} elsif( $doctype =~ /^strict$/i ){
		$DOCTYPE = $STRICT;

	} elsif( $doctype =~ /^html5$/i ){
		$DOCTYPE = $HTML5;

	} elsif( $doctype =~ /^none$/i ){
		$DOCTYPE = "";

	} elsif( $doctype =~ /^$/i ){
		$DOCTYPE = $HTML5;

	} else {
		croak "CGI::Template : Invalid doctype argument passed to new(): $doctype";
	}

	my $path = $ENV{PATH_INFO};
	if( $path ){
		$path =~ s/^\/+//;
		$path =~ s/\/+$//;
	}

	my $self = {
		path      => $path, 
		templates => $template_dir,
	};
	my $template = &_check_template( $template_dir );
	$self->{template} = $template if( $template );

	bless($self, $class);
	return $self;

}




sub header {
	my $self = shift;
	my %passed_hash = @_;

	my $header = "Content-type: text/html\n";

	my $redirect = 0;
	foreach my $i (keys %passed_hash){
		if( $i =~ /-cookie/ ){
			$header .= "Set-Cookie: " . $passed_hash{$i} . "\n";

		} elsif( $i =~ /-content-type/ ) {
			$header =~ s/text\/html/$passed_hash{$i}/;		

		} elsif( $i =~ /-redirect/ ) {
			$header =~ s/Content-type: .*?\n/Status: 302 Found\nLocation: $passed_hash{$i}\n/m;
			$redirect++;

		} else {
			$header .= "$i: " . $passed_hash{$i} . "\n";
		}
	}

	$header .= "\n";
	$header .= $DOCTYPE unless $redirect;

	return $header;
}

sub path {
	my $self = shift;
	return $self->{path};
}


sub content {
	my $self = shift;
	my %hash = @_;

	croak "CGI::Template : No template set!" unless defined $self->{template};

	my $data = $self->{template};

	foreach my $i (keys %hash){
		$data =~ s/#!$i!#/$hash{$i}/g;
	}

	return $data;
}

sub replace_template {
	my $self = shift;
	my $template = shift;
	$self->{template} = $template;
}

sub error {
	my $self = shift;
	my $message = shift;

	my $template_dir = $self->{templates};

	my $error_template = '';
	if( -e "$template_dir/error.html" ){
		my $fail = 0;
		open INFILE, "$template_dir/error.html" or $fail = 1;
		if( $fail ) { croak "CGI::Template : Couldn't open error template: $template_dir/error.html"; return 0; }
		{
			local $/;
			$error_template = <INFILE>;



( run in 0.306 second using v1.01-cache-2.11-cpan-ba708fea25c )