NAME

CGI::Application::PageBuilder - Simplifies building pages with multiple templates in CGI::Application.

SYNOPSIS

This module is built on the idea that building complex web pages with the default CGI::Application method can get to be a real mess. I personally found it much easier to build pages from many different smaller templates than to try to create one large template. This is especially true when displaying large sets of data from a database where you would be filling in a lot of <TMPL_VAR>s for each cell. This module aims to make that process a little easier.

So instead of:

 sub run_mode {
 	my $self = shift;
 	my $header = $self->load_tmpl( 'header.tmpl' )->output();
 	my $html;

 	my $start = $self->load_tmpl( 'view_start.tmpl' );
 	$start->param( view_name => 'This View' );
 	$html .= $start->output();

	 my $db = MyApp::DB::Views->retrieve_all(); # Class::DBI
	 while ( my $line = $db->next() ) {
		 my $template = $self->load_tmpl( 'view_element.tmpl' );
		 $template->param( name => $line->name() );
		 $template->param( info => $line->info() );
		 $html .= $template->output();
	 }
	 $html .= $self->load_tmpl( 'view_end.tmpl' )->output();
	 $html .= $self->load_tmpl( 'footer.tmpl' )->output();
	 return $html;
 }

You can do this:

 sub run_mode {
  	my $self = shift;
 	my $page = new CGI::Application::PageBuilder(
                    Header => 'header.tmpl',
                    Footer => 'footer.tmpl',
                    Super => $self );
 	$page->template( 'view_start.tmpl' );

 	my $db = MyApp::DB::Views->retrieve_all();
 	while( my $line = $db->next() ) {
		 $page->template( 'view_element.tmpl' );
		 $page->param( name => $line->name() );
		 $page->param( info => $line->info() );
 	}
 	$page->template( 'view_end.tmpl' );
 	return $page->output();
 }

Which arguably looks much cleaner.

The Super argument to new() allows the module to use the HTML::Template object already inside your CGI::Application subclass. Therefore, all template() calls expect filenames relative to the path you would have set in the setup() method of your CGI::Application subclass.

METHODS

new

my $page = new CGI::Application::PageBuilder( Header => 'header.tmpl', Footer => 'footer.tmpl', Super => $self );

Both Header and Footer are optional arguments. Super is required.

loose

$page->loose( value );

Value is 0 or 1 depending on whether you want die_on_bad_params enabled or not. See HTML::Template for more information. The default behavior is to have strict templates since this is also the default for HTML::Template.

template

$page->template( 'the_template_to_use.tmpl' );

Adds the template to the page and sets it as the next template to apply param to.

param

$page->param( name, value );

Sets the value for the param in the template. This applies to the last template loaded by template().

output

return $page->output();

Returns the HTML of the built page.

TODO

Needs actual tests.

There is probably a much more elegant way to do this.

At the moment param() automatically tries to add the parameter to the last template loaded. It might be nice to have a simple way of adding a parameter to an already loaded template. Some might possibly prefer to load all of their templates at once and then add parameters later on in the code. Perhaps something like:

my $page = new CGI::Application::PageBuilder(
	Header => 'header.tmpl',
	Footer => 'footer.tmpl',
	Super => $self );
$page->template( 'one.tmpl' );
$page->template( 'two.tmpl' );

...

$page->param( 'one.tmpl', param, value );
$page->param( 'one.tmpl', param, value );

etc.

AUTHOR

Clint Moore <cmoore@cpan.org>

LICENSE AND COPYRIGHT

Copyright (c) 2005, Clint Moore <cmoore@cpan.org>.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.