Apache-SimpleTemplate

 view release on metacpan or  search on metacpan

SimpleTemplate.pm  view on Meta::CPAN

				print $h . ': ' . $s->{headerref}->{$h} . "\n";
			}
		}

		if ($r) {
			# set my status and content_type...
			$r->status($s->{status});
			$r->content_type($s->{content_type});
			$r->send_http_header;
		}
		else {
			print 'Content-type: '. $s->{content_type} . "\n\n";
		}

		$s->{_header_sent} = 1;

		print STDERR "-------- Apache::SimpleTemplate STATUS $s->{status}\n" if ($s->{debug} > 1);
		print STDERR "-------- Apache::SimpleTemplate CTYPE  $s->{content_type}\n" if ($s->{debug} > 1);

	}

	if ($r) {
		# send the document if we're OK
		if ($s->{status} == 200) { $r->print(${ $s->{out} }); }
		elsif ($s->{debug} > 0) { $r->print($s->{_error}); }
	}
	else {
		if ($s->{status} == 200) { print ${ $s->{out} }; }
		elsif ($s->{debug} > 0) { print($s->{_error}); }
	}

	${ $s->{out} } = '';
}


#
# render
#
# takes a full path to a template
#

sub render {

	my $s = shift;

	if (!defined($s->{out})) { ${ $s->{out} } = ''; }

	my $inref = $s->{inref};
#	my $headerref = $s->{headerref};

	my $package = 'Apache::SimpleTemplate::Template'.$_[0];
	$package =~ s/\//\:\:/g;
	$package =~ s/[^\w\:]/_/g;
	print STDERR "-- RENDERING ($package)\n" if ($s->{debug} > 1);

	my $fun = $_cache{$_[0]};
	my $usecache = $fun ? 1 : $s->{cache};

	# check for updated template
	if ($fun && ($s->{reload} > 0)) {
		my $filetime = (stat($_[0]))[9];
		if ($_cache_time{$_[0]} < $filetime) {
			print STDERR "-- RELOADING ($package)\n" if ($s->{debug} > 1);
			$fun = undef;
		}
	}

	# check for cache
	if ($fun) {
		print STDERR "-- CACHE HIT ($package)\n" if ($s->{debug} > 1);
	} 
	else {
		print STDERR "-- COMPILING ($package)\n" if ($s->{debug} > 1);

		$fun = $s->compile($_[0]);
		if ($fun && ($usecache>0))  { 
			if ($s->{reload} > 0) { $_cache_time{$_[0]} = time; }
			$_cache{$_[0]} = $fun; 
		}
	}

	#print STDERR "-- SETTING STATUS for $s to $s->{status}\n" if $s->{debug};
	if (!(ref $fun)) {
		if ($fun != 200) { $s->{status} = $fun; return ''; }
		my $packagefun = $package.'::____st_go_';
		$fun = \&$packagefun;
	}

	return &$fun($s);

}

#
# compile a template
#
# load from file:
#   $s->compile('/www/myhost.com/foo.stml');
# pass in body:
#   $s->compile($template, 1);
#

sub compile {

	my $s = shift;

	my $template = $_[1] ? $_[0] : &load($_[0]);
	if (!defined $template) { $s->{_error} .= "Not Found: $_[0]\n"; return 404; }
	
	my $block_begin = $s->{block_begin} || $DEFAULT_BLOCK_BEGIN;
	my $block_end = $s->{block_end} || $DEFAULT_BLOCK_END;
	print STDERR "-- DELIM: $block_begin $block_end\n" if $s->{debug} > 2;

	$block_begin =~ s/([^\w])/\\$1/g;
	$block_end =~ s/([^\w])/\\$1/g;
	
	my $eval = '';
	my $precode = '';
	
	my $package = 'Apache::SimpleTemplate::Template'.$_[0];
	$package =~ s/\//\:\:/g;
	$package =~ s/[^\w\:]/_/g;



( run in 2.065 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )