CGI-Application-Plus

 view release on metacpan or  search on metacpan

lib/CGI/Application/Magic.pm  view on Meta::CPAN

      ... do_something_useful ...
      ... no_need_to_set_page ...
      ... returned_value_will_be_ignored ...
    }
    
    # package where Template::Magic will looks up
    package WebApp::Lookups ;
    
    # this value will be substituted to each
    # 'app_name' label in each template that include it
    our $app_name = 'WebApp 1.0' ;
    
    # same for each 'Time' label
    sub Time { scalar localtime }
    
    # and same for each 'ENV_table' block
    sub ENV_table
    {
      my ($self,        # $self is your WebApp object
          $zone) = @_ ; # $zone is the Template::Magic::Zone object
      my @table ;
      while (my @line = each %ENV)
      {
        push @table, \@line
      }
      \@table ;
    }

An auto-magically used template (it contains the 'ENV_table block', and the 'app_name' and 'Time' labels)

    <html>
    
    <head>
    <meta http-equiv=content-type content="text/html;charset=iso-8859-1">
    <title>ENVIRONMENT</title>
    <style media=screen type=text/css><!--
    td   { font-size: 9pt; font-family: Arial }
    --></style>
    </head>
    
    <body bgcolor=#ffffff>
    <table border=0 cellpadding=3 cellspacing=1 width=100%>
    <tr><td bgcolor=#666699 nowrap colspan=2><font size=3 color=white><b>ENVIRONMENT</b></font></td></tr>
    <!--{ENV_table}-->
    <tr valign=top>
    <td bgcolor=#d0d0ff nowrap><b>the key goes here</b></td>
    <td bgcolor=#e6e6fa width=100%>the value goes here</td>
    </tr>
    <!--{/ENV_table}-->
    </table>
    Generated by <!--{app_name}--> - <!--{Time}-->
    </body>
    
    </html>


See also the F<'magic_example'> directory in this distribution

=head1 DESCRIPTION

This module transparently integrates C<CGI::Application::Plus> and C<Template::Magic> in a very handy, powerful and flexible framework that can save you a lot of coding, time and resources.

B<Note>: Knowing L<CGI::Application::Plus> and L<Template::Magic> could help to better understand this documentation ;-).

B<IMPORTANT NOTE>: If you write any script that rely on this module, you better send me an e-mail so I will inform you in advance about eventual planned changes, new releases, and other relevant issues that could speed-up your work. 

=head2 Why CGI::Application::Plus and Template::Magic?

=over

=item *

L<Template::Magic|Template::Magic> is a module that can auto-magically look up the runtime values in packages, hashes and blessed objects

=item *

It has the simplest possible template syntax (idiot-proof), and it is written in pure perl (no compiler needed), so it is perfect to be used by (commercial) user-customizable CGI applications.

=item *

It uses minimum memory because it prints the output while it is produced, avoiding to collect in memory the whole (and sometime huge) content.

=item *

L<CGI::Application::Plus|CGI::Application::Plus> allows maximum flexibility of the CGI application structure. Without it this framework wouldn't be possible.

=back

=head2 Concept

The C<CGI::Application> philosophy is very simple: each runmethod is organized to produce its own output page. Very easy to understand concept, but not so flexible to utilize. In real world jobs, if your runmethods use templates to produce the output...

A CGI application can have a lot of run modes, and you have to creates a lot of different run methods, a lot of different templates, set a different hash for each one of them to discover very soon that sometimes the hashes share 50% of keys and value...

Another stupid and redundant thing is that frequently you have a runmode named e.g. C<'foo'>, its relative run method named C<foo()> and its relative template file named F<'foo.html'>. May be this happens because you have a too simple mind and you sh...

Well, you understand the problem... the code grows too much with too much redundancy, maintaining it may be very difficult, sometimes it becomes a real mess, and usually you have no enough time to think about how to organize things in a different way...

This module organizes the output production in a far more flexible and simpler way and wipes out each stressing redundancy making smart use of defaults (always overridable).

=head2 Run modes

This module allows you to move all the template-related stuff out of the runmethods so that B<the output production can be not only runmode driven, but also template and label driven>. In other words: a runmode is not the output production center any...

These are the points that explain you the concept:

=over

=item *

A C<CGI::Application::Magic> implementation (i.e. your application module using this one as a base class) can completely avoid to create the template object because it is internally created and managed (anyway you can override its constructor by comp...

=item *

A runmethod does not need to set any hash to pass its runtime values, because they can be automagically found by the lookup of the template system, (anyway you can decide to produce the values that are exclusive for that particular runmethod and pass...

=item *

The runmethod does not need even to set the template name, because the run mode base name will be used to find a template with the same name, (anyway you can pass a different name as you need).

=item *



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