Apache-SimpleTemplate
view release on metacpan or search on metacpan
SimpleTemplate.pm view on Meta::CPAN
}
# escape single quotes (') and backslashes (\) with \' and \\
sub quote_escape {
my $s = shift;
if (ref $s) { $s = shift; }
return undef unless defined($s);
$s =~ s/([\'\\])/\\$1/gs;
return $s;
}
# escape single quotes (') and backslashes (\) with \' and \\, newlines and cr's with \n \r
sub js_escape {
my $s = shift;
if (ref $s) { $s = shift; }
return undef unless defined($s);
$s =~ s/([\'\\])/\\$1/gs;
$s =~ s/\n/\\n/g;
$s =~ s/\r/\\r/g;
return $s;
}
#
# parse_form
#
# try to get the form data every which way..
# %form = $r->args; loses multiple checkboxes....
# and doesn't parse a QUERY STRING in a POST. :(
#
sub parse_form {
my $s = shift;
my ($r) = @_;
#print STDERR "PARSE FORM! $r\n";
my (%form, @form);
if ($r && $r->args && ref($r->args)) {
@form = $r->args;
}
# elsif ($ENV{QUERY_STRING}) {
# foreach my $pair (split('&', $ENV{QUERY_STRING})) {
elsif (($r && $r->args) || $ENV{QUERY_STRING}) {
foreach my $pair (split('&', (($r && $r->args) ? $r->args : $ENV{QUERY_STRING}))) {
my ($k, $v) = split('=', $pair);
push (@form, &decode($k), &decode($v));
}
}
if (($r) && ($r->method() eq 'POST') && ($r->header_in('Content-Length') > 0)) {
# handle upload posts
if ($r->header_in('Content-Type') =~ m/multipart\/form-data/i) {
use CGI;
$CGI::DISABLE_UPLOADS = 0;
my $cgi = CGI->new();
foreach my $k (keys %{$cgi->Vars}) { $form{$k} = $cgi->param($k); }
use CGI::Upload;
my $upload = CGI::Upload->new({ query => $cgi });
$s->{upload} = $upload;
}
# handle other posts
else {
push @form, $r->content();
}
# blank this so we don't think there's more to read
$r->header_in('Content-Length', 0);
}
for (my $i = 0; $i < $#form; $i += 2) {
## $r->content returns empty things
## as undefs instead of as empty strings...
unless (defined $form[$i+1]) { $form[$i+1] = ''; }
# print STDERR " GOT: $form[$i] = "+$form[$i+1]+"\n";
$form{$form[$i]} = $form{$form[$i]} ?
$form{$form[$i]}."\0".$form[$i+1] : $form[$i+1];
}
return \%form;
}
#
# load()
#
# given a full path/filename,
# return the file as a string.
#
sub load {
my ($filename) = @_;
my $ret;
local $/ = undef;
unless (open FILE, $filename) {
print STDERR "** Apache::SimpleTemplate: Unable to load $filename: $!\n";
return undef;
}
while(<FILE>) { $ret .= $_; }
return ($ret);
}
1;
__END__
=head1 NAME
Apache::SimpleTemplate
SimpleTemplate.pm view on Meta::CPAN
=head3 <%= $$inref{foo}; %>
prints the value of the CGI/form input variable 'foo'.
=head3 <% $s->header('Location','/'); $s->status(302); return; %>
ends execution of the template and redirects browser to '/'.
=head3 <% $s->content_type('text/xml'); %>
sets our content-type to 'text/xml' instead of default 'text/html';
=head3 <%: _perl_code_ %> DEPRECATED
evaluates the perl code, and the block gets replaced by the last
value returned in the perl code, or $out if defined. (included
mostly for backward compatability-- it's better to use a mixture
of <% %> and <%= %> blocks.)
(mnemonic: '<%: %>' is like a combination of '<% %>' and '<%= %>'.)
=head1 DESCRIPTION
Apache::SimpleTemplate is *another* Template-with-embedded-Perl package
for mod_perl. It allows you to embed blocks of Perl code into text
documents, such as HTML files, and have this code executed upon HTTP
request. It should take moments to set-up and learn; very little knowledge
of mod_perl is necessary, though some knowledge of Apache and perl is
assumed.
This module is meant to be a slim and basic alternative to more fully
featured packages like Apache::Embperl, Apache::ASP, or TemplateToolkit,
and more of a mod_perl counterpart to JSP or PHP. You may wish to compare
approaches and features of the other perl templating schemes, and consider
trade-offs in funcionality, implementation time, speed, memory
consumption, etc. This module's relative lack of "features" is meant to
improve both its performance and its flexibility.
Apache::SimpleTemplate has no added programming syntax, relying simply
on perl itself for all programming logic in the templates. It should
run with a very small memory footprint and little processing over-head.
Templates get compiled into perl packages (or subroutines), and the
caching and preloading options can help you increace speed and reduce
memory consumption. SimpleTemplate is also designed for extension
through subclasses, into which you can add the functionality you want.
=head1 INSTALLATION
The only requirement is mod_perl**. To install Apache::SimpleTemplate, run:
perl Makefile.PL
make
make install
(** Version 0.06 works with mod_perl2 in compatibility mode. Older versions
may work better with mod_perl1. The CGI library is needed if you want
file upload support.)
Then, to test it with Apache/mod_perl:
1) put the httpd.conf lines above into your httpd.conf
2) restart apache
3) try putting an example template from below into your document root
4) point your browser at the example
=head1 EXAMPLES
=head2 template "example.stml"
<%!
my $foo = 'working!';
sub not_installed_properly { return $foo;}
%>
<html>
<body bgcolor="ffffff">
<h2>Apache::SimpleTemplate seems to be <%= ¬_installed_properly(); %> </h2>
</body>
</html>
=head2 template "/printenv.stml"
<table border=3>
<tr><th colspan=2 align=left>Environment variables</th></tr>
<% foreach my $e (sort keys(%ENV)) { %>
<tr>
<td><strong><%=$e%></strong></td>
<td><%=$ENV{$e};%></td>
</tr>
<% } %>
</table>
<table border=3>
<tr><th colspan=2 align=left>CGI Arguments</th></tr>
<% foreach my $e (sort keys %$inref) { %>
<tr>
<td><strong><%=$e%></strong></td>
<td><%=$$inref{$e};%></td>
</tr>
<% } %>
</table>
=head2 subclass "MySimpleTemplate"
# in httpd.conf should set the handler: "PerlHandler +MySimpleTemplate"
# in your template you can call: "<%= $s->my_method %>"
package MySimpleTemplate;
use Apache::SimpleTemplate ();
( run in 1.654 second using v1.01-cache-2.11-cpan-b16cb0d3907 )