CGI-Application
view release on metacpan or search on metacpan
lib/CGI/Application.pm view on Meta::CPAN
# Set a complete set of headers
%set_headers = $webapp->header_props(-type=>'image/gif',-expires=>'+3d');
# clobber / reset all headers
%set_headers = $webapp->header_props({});
# Just retrieve the headers
%set_headers = $webapp->header_props();
The C<header_props()> method expects a hash of CGI.pm-compatible
HTTP header properties. These properties will be passed directly
to the C<header()> or C<redirect()> methods of the query() object. Refer
to the docs of your query object for details. (Be default, it's L<CGI>.pm).
Calling header_props with an empty hashref clobber any existing headers that have
previously set.
C<header_props()> returns a hash of all the headers that have currently been
set. It can be called with no arguments just to get the hash current headers
back.
To add additional headers later without clobbering the old ones,
see C<header_add()>.
B<IMPORTANT NOTE REGARDING HTTP HEADERS>
It is through the C<header_props()> and C<header_add()> method that you may modify the outgoing
HTTP headers. This is necessary when you want to set a cookie, set the mime
type to something other than "text/html", or perform a redirect. The
header_props() method works in conjunction with the header_type() method.
The value contained in header_type() determines if we use CGI::header() or
CGI::redirect(). The content of header_props() is passed as an argument to
whichever CGI.pm function is called.
Understanding this relationship is important if you wish to manipulate
the HTTP header properly.
=head3 header_type()
$webapp->header_type('redirect');
$webapp->header_type('none');
This method used to declare that you are setting a redirection header,
or that you want no header to be returned by the framework.
The value of 'header' is almost never used, as it is the default.
B<Example of redirecting>:
sub some_redirect_mode {
my $self = shift;
# do stuff here....
$self->header_type('redirect');
$self->header_props(-url=> "http://site/path/doc.html" );
}
To simplify that further, use L<CGI::Application::Plugin::Redirect>:
return $self->redirect('http://www.example.com/');
Setting the header to 'none' may be useful if you are streaming content.
In other contexts, it may be more useful to set C<$ENV{CGI_APP_RETURN_ONLY} = 1;>,
which suppresses all printing, including headers, and returns the output instead.
That's commonly used for testing, or when using L<CGI::Application> as a controller
for a cron script!
=cut
sub html_tmpl_class {
my $self = shift;
my $tmpl_class = shift;
# First use? Create new __ERROR_MODE
$self->{__HTML_TMPL_CLASS} = 'HTML::Template' unless (exists($self->{__HTML_TMPL_CLASS}));
if (defined $tmpl_class) {
$self->{__HTML_TMPL_CLASS} = $tmpl_class;
}
return $self->{__HTML_TMPL_CLASS};
}
sub load_tmpl {
my $self = shift;
my ($tmpl_file, @extra_params) = @_;
# add tmpl_path to path array if one is set, otherwise add a path arg
if (my $tmpl_path = $self->tmpl_path) {
my @tmpl_paths = (ref $tmpl_path eq 'ARRAY') ? @$tmpl_path : $tmpl_path;
my $found = 0;
for( my $x = 0; $x < @extra_params; $x += 2 ) {
if ($extra_params[$x] eq 'path' and
ref $extra_params[$x+1] eq 'ARRAY') {
unshift @{$extra_params[$x+1]}, @tmpl_paths;
$found = 1;
last;
}
}
push(@extra_params, path => [ @tmpl_paths ]) unless $found;
}
my %tmpl_params = ();
my %ht_params = @extra_params;
%ht_params = () unless keys %ht_params;
# Define our extension if doesn't already exist;
$self->{__CURRENT_TMPL_EXTENSION} = '.html' unless defined $self->{__CURRENT_TMPL_EXTENSION};
# Define a default template name based on the current run mode
unless (defined $tmpl_file) {
$tmpl_file = $self->get_current_runmode . $self->{__CURRENT_TMPL_EXTENSION};
}
$self->call_hook('load_tmpl', \%ht_params, \%tmpl_params, $tmpl_file);
my $ht_class = $self->html_tmpl_class;
eval "require $ht_class;" || die "require $ht_class failed: $@";
# let's check $tmpl_file and see what kind of parameter it is - we
# now support 3 options: scalar (filename), ref to scalar (the
( run in 2.851 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )