Apache-SimpleTemplate

 view release on metacpan or  search on metacpan

SimpleTemplate.pm  view on Meta::CPAN

     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 <%= &not_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 ();
  our @ISA = qw(Apache::SimpleTemplate);

  # handler() must be defined, as it is not a method.
  # instantiate this class, and call SimpleTemplate's handler:
  sub handler {
      my $r = shift;
      my $s = new MySimpleTemplate($r);
      #$s->block_begin('<%');
      #$s->block_end('%>');

      # you can make additional steps/logic here, including:
      #     set $s->file() for a template to use
      #     change $s->status()
      #     add headers w/ $s->header()

      return Apache::SimpleTemplate::handler($r, $s);
  }
  
  sub my_method {
      my $self = shift;
      return 'this is my_method.';
  }
  1;



( run in 1.541 second using v1.01-cache-2.11-cpan-39bf76dae61 )