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

View: New views
2 Messages — Rating Filter:   Alert me  

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

by Rhesa Rozendaal-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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) {



#####  CGI::Application community mailing list  ################
##                                                            ##
##  To unsubscribe, or change your message delivery options,  ##
##  visit:  http://www.erlbaum.net/mailman/listinfo/cgiapp    ##
##                                                            ##
##  Web archive:   http://www.erlbaum.net/pipermail/cgiapp/   ##
##  Wiki:          http://cgiapp.erlbaum.net/                 ##
##                                                            ##
################################################################


Re: tiny refactoring (extract hard-coded HTML::Template name)

by Mark Stosberg :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Thanks for the contribution, Rhesa. Come feedback is below.

On Wed, 07 May 2008 15:12:37 +0200
Rhesa Rozendaal <perl@...> wrote:

> 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().

Opinions from others?

Considering how little load_tmpl() changes, I don't mind that this is solved
through plugins, which in this case are already written, and it would be clear
how to write yet another.

I know a lot of people are using CGI::App with Template::Toolkit or other
templating systems, so I'm hestitent to add an H::T specific method, when the
plugin approach is already established to address this.

However, I'm persuadable of there are other points of view of this.

  Mark

> --- 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) {
>
>
>
> #####  CGI::Application community mailing list  ################
> ##                                                            ##
> ##  To unsubscribe, or change your message delivery options,  ##
> ##  visit:  http://www.erlbaum.net/mailman/listinfo/cgiapp    ##
> ##                                                            ##
> ##  Web archive:   http://www.erlbaum.net/pipermail/cgiapp/   ##
> ##  Wiki:          http://cgiapp.erlbaum.net/                 ##
> ##                                                            ##
> ################################################################
>
>


--
 . . . . . . . . . . . . . . . . . . . . . . . . . . .
   Mark Stosberg            Principal Developer  
   mark@...     Summersault, LLC    
   765-939-9301 ext 202     database driven websites
 . . . . . http://www.summersault.com/ . . . . . . . .



#####  CGI::Application community mailing list  ################
##                                                            ##
##  To unsubscribe, or change your message delivery options,  ##
##  visit:  http://www.erlbaum.net/mailman/listinfo/cgiapp    ##
##                                                            ##
##  Web archive:   http://www.erlbaum.net/pipermail/cgiapp/   ##
##  Wiki:          http://cgiapp.erlbaum.net/                 ##
##                                                            ##
################################################################