Apache-SimpleTemplate

 view release on metacpan or  search on metacpan

SimpleTemplate.pm  view on Meta::CPAN

	my $r = shift;
	my $self = {};

	bless($self, $class);

	if (ref($r) =~ m/Apache/) {
		$self->{file} = $r->dir_config('SimpleTemplateFile');
		$self->{block_begin} = $r->dir_config('SimpleTemplateBlockBegin');
		$self->{block_end} = $r->dir_config('SimpleTemplateBlockEnd');
		$self->{content_type} = $r->dir_config('SimpleTemplateContentType');
		$self->{cascade_status} = $r->dir_config('SimpleTemplateCascadeStatus');

		$self->{cache} = $r->dir_config('SimpleTemplateCache');
		$self->{reload} = $r->dir_config('SimpleTemplateReload');
		$self->{debug} = $r->dir_config('SimpleTemplateDebug');
	}
	if (!defined $self->{cache}) { $self->{cache} = $DEFAULT_CACHE_FLAG; }
	if (!defined $self->{reload}) { $self->{reload} = $DEFAULT_RELOAD_FLAG; }
	if (!defined $self->{block_begin}) { $self->{block_begin} = $DEFAULT_BLOCK_BEGIN; }
	if (!defined $self->{block_end}) { $self->{block_end} = $DEFAULT_BLOCK_END; }
	if (!defined $self->{file}) { $self->{file} = ($ENV{SCRIPT_NAME} . ($ENV{PATH_INFO}||'') ); }
	if (!defined $self->{content_type}) { $self->{content_type} = $DEFAULT_CONTENT_TYPE; }
	if (!defined $self->{cascade_status}) { $self->{cascade_status} = $DEFAULT_CASCADE_STATUS; }
	if (!defined $self->{debug}) { $self->{debug} = $DEFAULT_DEBUG_LEVEL; }

	print STDERR "---- NEW SimpleTemplate OBJECT FOR $self->{file}\n" if ($self->{debug} > 1);
	
	$self->{r} = $r;
	$self->{inref} = (ref($r) eq 'HASH') ? $r : $self->parse_form($r);
	$self->{headerref} = {};
	$self->{status} = 200;

	return $self;

SimpleTemplate.pm  view on Meta::CPAN


# getters/setters
sub block_begin { my $s = shift; return $_[0] ? $s->{block_begin} = $_[0] : $s->{block_begin}; }
sub block_end { my $s = shift; return $_[0] ? $s->{block_end} = $_[0] : $s->{block_end}; }
sub file { my $s = shift; return $_[0] ? $s->{file} = $_[0] : $s->{file}; }
sub debug { my $s = shift; return $_[0] ? $s->{debug} = $_[0] : $s->{debug}; }
sub reload { my $s = shift; return $_[0] ? $s->{reload} = $_[0] : $s->{reload}; }
sub cache { my $s = shift; return $_[0] ? $s->{cache} = $_[0] : $s->{cache}; }
sub status { my $s = shift; return $_[0] ? $s->{status} = $_[0] : $s->{status}; }
sub content_type { my $s = shift; return $_[0] ? $s->{content_type} = $_[0] : $s->{content_type}; }
sub cascade_status { my $s = shift; return $_[0] ? $s->{cascade_status} = $_[0] : $s->{cascade_status}; }


#
# header
# 
# safely add a header (without squashing an existing entry.)
#
sub header {
	my ($s, $name, $value) = @_;
	my $cur = $s->{headerref}->{$name};

SimpleTemplate.pm  view on Meta::CPAN

#   $s->include('/path/relative/to/docroot.stml');
#

sub include {

	my $s = shift;
	
	print STDERR "---- Apache::SimpleTemplate::include FROM $s->{file} FOR $_[0]\n" if ($s->{debug} > 1);
	my $tmp = $s->{status};
	$s->render($ENV{DOCUMENT_ROOT}.$_[0]);
	$s->status($tmp) if ($s->{cascade_status} == 0);

}



#
# preload a template into memory
# takes a full path
#

SimpleTemplate.pm  view on Meta::CPAN


  $s->debug()              -- get or set the debug level (0=quiet - 3=verbose)
  $s->debug(1)

  $s->reload()             -- get or set the reload flag (0 or 1)
  $s->reload(1)

  $s->cache()              -- get or set the caching flag (0 or 1)
  $s->cache(1)

  $s->cascade_status()     -- get or set flag for cascading status codes 
  $s->cascade_status(0)       from included templates


=head2 other methods/functions (mostly useful in templates):

  $s->content_type('text/xml')   -- set our content-type to something
                                    (must be done before any call to flush().)
  $s->status(302)                -- set our status to something other than 200
                                    (must be done before any call to flush().)
  $s->header($name,$value)       -- add an outgoing header. (can add multiple 
                                    of the same name.)

SimpleTemplate.pm  view on Meta::CPAN

  The compilation process tries to keep the line numbers consistent with
  the template, but <%! %> declarations/definitions that are not at the
  top of the template may throw line numbers off.

  Any additional variables you wish to use must be declared (with 'my').
  If you declare them in <%! %> or <% %> blocks, they will be accessible
  in later blocks.

  Included sub-templates receive the same instance of $s, so they have the 
  same $inref, etc. Thus, they can also set headers, change status, etc.
  (Turn off "cascade_status" to prevent the latter.)


=head2 template debugging

  SimpleTemplateDebug / $s->debug() values have the following meanings
    0 quiet, errors logged to error_log
    1 quiet, errors also sent to browser
    2 info on requests, includes, status codes, etc.
    3 info on template compilation. (noisy)
    4 verbose info on template compilation. (extremely noisy)



( run in 0.583 second using v1.01-cache-2.11-cpan-49f99fa48dc )