CGI-Template
view release on metacpan or search on metacpan
lib/CGI/Template.pm view on Meta::CPAN
Sets the path to the templates directory. By default this is a directory called C<templates> under the directory from which the script is being executed (typically C<cgi-bin>).
=item request
Specifies the request method to be used. Possible values are C<get> or C<post>. If set, CGI::Template will require that all requests to the script be of the type specified. Any other types of requests will result in a call to error().
=back
=head2 $t->header()
Returns an HTTP Content-type header. By default, the mimetype returned is C<text/html> but this can be specified. C<header()> accepts options in the form of key/value pairs:
=over 4
=item -content-type
Sets the MIME type value in the header. Some examples:
print $t->header( -content-type => "image/png" );
print $t->header( -content-type => "text/plain" );
=item -redirect
Causes header() to return a HTTP C<Location> header that will redirect the user to a new page. Example:
print $t->header( -redirect => "/cgi-bin/new_location" );
=item -cookie
Sets a cookie. Example:
print $t->header( -cookie => $cookie );
=item [any other key]
Any other key passed will result in the addition of the key and value to the header. Example:
print $t->header( Expires => $date );
will result in:
Content-type: text/html
Expires: [date specifed]
=back
=head2 $t->content()
Returns the final HTML output. Accepts placehoders and their replacements as key/value pairs. These replacements will be made throughout the current template. Example:
If CGI::Template is called from a script called "welcome":
#!/usr/bin/perl -w
use strict;
use CGI::Template;
my $t = new CGI::Template;
my $title = "Welcome";
my $menu = "Menu";
my $text = "Hello world.";
print $t->header();
print $t->content(
TITLE => $title,
MENU => $menu,
TEXT => $text,
);
And the contents of cgi-bin/templates/welcome.html are as follows:
<html>
<head>
<title>#!TITLE!#</title>
</head>
<body>
<div id="menu">#!MENU!#</div>
<div id="content">#!TEXT!#</div>
</body>
</html>
Then the resulting output of the "welcome" script will be:
Content-type: text/html
<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<div id="menu">Menu</div>
<div id="content">Hello world.</div>
</body>
</html>
=head2 $t->get_template()
Requres a string argument. Returns a template retrieved from the file named $string within the CGI::Template directory. This is useful to reduce the amount of time spent editing template content that is used on many pages throughout your web applica...
my $menu = $t->get_template( "menu" );
...
print $t->content(
...
MENU => $menu,
...
);
=head2 $t->replace_template()
Requires a string argument. Replaces the current template with the provided string argument. This is useful if under some conditions your script needs to output a different template than usual. Often used with get_template(). Example:
if( $other ){
my $new_template = $t->get_template( "other" );
$t->replace_template( $new_template );
print $t->header();
print $t->content(
PLACEHOLDER => $value,
...
);
exit;
}
=head2 $t->path()
Returns the remaining content after the script name in the URL. (i.e., the content of the environment variable C<PATH_INFO>).
=head2 $t->error()
Requres a string argument. Prints an HTTP header, an error message, and calls C<exit()>. Accepts a string argument which then replaces the placehoder MESSAGE in the error template: error.html Example:
$t->error( "Zip code must be numeric." ) if( $zipcode !~ /^\d+$/ );
=head1 SEE ALSO
There are many other HTML template systems available for Perl, and each one does things a little (or a lot) differently. If you'd like to freely mix Perl within your HTML you should check out L<HTML::Embperl>. If you'd like to be able to process lo...
=head1 AUTHOR
David J. Venable, E<lt>davidjvenable@gmail.comE<gt>, @davevenable
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2013 by David J. Venable
This library is free software; you can redistribute it and/or modify
( run in 2.281 seconds using v1.01-cache-2.11-cpan-cdf2f3d4e48 )