[cgiapp] tiny refactoring (extract hard-coded HTML::Template name)

Rhesa Rozendaal perl at rhesa.com
Wed May 7 09:12:37 EDT 2008


The diff (against 4.07_02) below extracts the hard-coded HTML::Template 
classname from load_tmpl.

This should make it easier to drop in a replacement module, and it will 
certainly simplify maintenance of both CGI::Application::Plugin::HTDot and 
your CGI::Application::Plugin::HTCompiled, since you no longer have to 
copy/paste the entire load_tmpl().

Happy hacking,

Rhesa



--- lib/CGI/Application.pm	2007-11-01 00:32:10.000000000 +0100
+++ refactor/CGI/Application.pm	2008-05-07 15:07:31.000000000 +0200
@@ -1412,7 +1412,11 @@ and features pre-and-post features, sing
  L<CGI::Application::Plugin::Stream> can help if you want to return a stream and
  not a file. It features a simple syntax and MIME-type detection.

+B<specifying the template class with html_tmpl_class()>

+If you have a template system that is API compatible to HTML::Template, it
+may be enough to override C<html_tmpl_class()>. This method should return
+the class name of your template system. The default simply returns
+"HTML::Template".



@@ -1441,6 +1445,8 @@ Here's an example stub for a load_tmpl()

  =cut

+sub html_tmpl_class { 'HTML::Template' }
+
  sub load_tmpl {
  	my $self = shift;
  	my ($tmpl_file, @extra_params) = @_;
@@ -1474,17 +1480,18 @@ sub load_tmpl {

      $self->call_hook('load_tmpl', \%ht_params, \%tmpl_params, $tmpl_file);

-    require HTML::Template;
+    my $ht_class = $self->html_tmpl_class;
+    eval "require $ht_class;";
      # let's check $tmpl_file and see what kind of parameter it is - we
      # now support 3 options: scalar (filename), ref to scalar (the
      # actual html/template content) and reference to FILEHANDLE
      my $t = undef;
      if ( ref $tmpl_file eq 'SCALAR' ) {
-        $t = HTML::Template->new_scalar_ref( $tmpl_file, %ht_params );
+        $t = $ht_class->new_scalar_ref( $tmpl_file, %ht_params );
      } elsif ( ref $tmpl_file eq 'GLOB' ) {
-        $t = HTML::Template->new_filehandle( $tmpl_file, %ht_params );
+        $t = $ht_class->new_filehandle( $tmpl_file, %ht_params );
      } else {
-        $t = HTML::Template->new_file($tmpl_file, %ht_params);
+        $t = $ht_class->new_file($tmpl_file, %ht_params);
      }

      if (keys %tmpl_params) {




More information about the cgiapp mailing list