in reply to Browser-viewable HTML::Template templates
Writing harness code at the same time you create your template files also has the added benefit of forcing me to think through the application and template functionality. Once I have a solid understanding of the functionality, it is a breeze to write a simple skeletal framework.
For creating web applications I use CGI::Application. Using this module, realistic harness code is easily created through the mechanism of run-modes. The more realistic the code is from the beginning, the more bug-free the final product will be. To illustrate what I'm talking about, here's an example of what a test harness for application and template development might look like:
package My::WebApp; use base qw/CGI::Application/; sub setup { my $self = shift; $self->start_mode('show_form'); $self->run_modes( show_form => 'show_form', list_results => 'list_results', show_detail => 'show_detail', ); } sub show_form { my $self = shift; my $tmpl = $self->load_tmpl('search_form.tmpl'); # Set up example data for template $tmpl->param('foo_checked' => '1'); return $tmpl->output(); } sub list_results { my $self = shift; my $tmpl = $self->load_tmpl('list.tmpl'); # Set up example data for template $tmpl->param('widget_loop' => [ { widget_name => 'widget_one', widget_id => '1' }, { widget_name => 'widget_two', widget_id => '2' }, { widget_name => 'widget_three', widget_id => '3' }, ]); return $tmpl->output(); } sub show_detail { my $self = shift; my $tmpl = $self->load_tmpl('detail_view.tmpl'); # Set up example data for template $tmpl->param('foo_id' => '1'); $tmpl->param('foo_name' => 'widget_one'); $tmpl->param('foo_checked' => 1); return $tmpl->output(); }
|
|---|